-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function names prefixed with an underscore are treated as comments #81
Comments
Variables with underscores in elixir are a way to mark them as unused. Hence we highlight them to show that. In your example there isn't any semantic reason to use an underscore in front of the function name. Its marked as a private function so there's no risk in other consumers of your api re-using your function. Honestly, I'm not sure that this is a bug. |
Yeah I'm fairly new to Elixir so I'm still working out naming conventions, for what it's worth this code is copied pretty much verbatim from Programing Elixir 1.3. But this is still a syntactically legal name for a function, so I'd say highlighting it as an ignored pattern match is a bug. |
@rkma I'm not sure how your PR resolves this. It marks everything starting with an underscore as an In order to solve this we need to not identify unused variables outside of pattern matching. That means we'll need to identify unused variables only inside of function arguments, Does anyone want to try their hand at this? |
@keathley Instead, I have found a band-aid solution which MIGHT suit for certain use cases. Current pattern for unused variable: \\b_(\\w*) Make it like this (could use some refinements): \\b_(\\w*)\\b(?!\\() with this negative-lookahead, it won't match for # match for this '_unused' binding
_unused = some_function(:arg1)
# not match for this function name
_underscored_function_call(:arg1) There are an obvious case which cannot be covered by this pattern: 0-arity function calls. # matching; cannot distinguish whether it is a variable or a function
_underscored_no_arity_function_call And I haven't come up with good solution to distinguish 0-arity function calls and variables/bindings (it is too hard for me to implement within regex pattern matching). Thus I call it band-aid solution. Of course I haven't tested against all of the existing coding styles and use cases. |
@ymtszw Now that functions without parens are being warned we might be able to solve this. One use case that you'll have to watch out for though is when you have a function that comes after a pipe like: In general I guess I'm not sure what the utility of prefixing a function with Let me know what you think. If this is still an issue and you want to have a go at a PR then that would be great. Otherwise I'd like to close this issue. 😄 👍 |
Well I am not so enthusiastic about this issue since at the end of the day, Although with Elixir 1.4+, function calls with explicit parens became popular in my sight, so my proposed "band-aid" solution is now actually okay to have, I assume. I'll post PR later after checking it works as I described. Concerning utility of @doc """
Callback invoked by Plug on every request.
"""
def call(conn, _opts) do
conn
|> prepare()
|> __match_route__(conn.method, Enum.map(conn.path_info, &URI.decode/1), conn.host)
|> Phoenix.Router.__call__()
end We MIGHT consider these double- |
I totally understand how frustrating some of this stuff can be 😄. Thanks for having a go at it. I'll go ahead and close this for now. We can revisit it later if we need to. |
Like the title says, a call to a function whose name begins with an underscore character, such as in the example above, is incorrectly treated as a comment.
Here's what I get when logging the scope at line 3, column 42:
The text was updated successfully, but these errors were encountered: