-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Warn when variable is being expanded to function call #3268
Comments
Strongly 👍. |
👍 |
+1 from me. |
I agree with @lpil and dislike removing opportunity to call zero-arity functions without parentheses. |
+1 from me as well |
It will push users to improve the quality of the code, as it will be less ambiguous, but I think it will affect the aesthetics of it. |
For a historical reference, how it (variable is used as function call) became a problem one more time: |
Small example:
|
I've got a little collection of thoughts on this subject: Would this mean you'd have to use parens in pipelines as well? I really like the way pipelines work without parens on 0-arity functions, for example: my_var
|> do_something
|> do_something_else
|> do_a_third_thing(1, 2) To suppress this warning, it looks like you'd have to rewrite the above as: my_var
|> do_something()
|> do_something_else()
|> do_a_third_thing(1, 2) In this case, the first example makes it obvious that values are being piped to functions (you can't pipe to anything else), and looks a little sweeter in my opinion. Another thing to think about is the way a human can read Elixir code. def n do
10
end
def random_number do
:random.uniform(10)
end Obviously this is a contrived example, but one can think through this code as "I am defining However, in the case of something like Lastly, if all functions with 0-arity should be called with parens, wouldn't it make more sense for the preferred style of defining a function to be like this: def start() do
:ok
end
start() This would be more consistent than having a definition with one style and a call with another. Anyway, my main point is that semantically, different styles make more sense in different scenarios. I'm not really sure what the answer is, as I definitely understand what @josevalim is saying (it's thrown me off before too), but those are my two cents 😄 |
No, because are macros and the module compiler works on the AST after the macro is expanded. None of the code samples you have shown would need to change. |
Sorry, closed by mistake. |
Interesting, thanks for the explanation. |
Still +1 to the original issue. I have adopted the following style for function calls long ago:
I suggest everyone else do the same. Even if this issue doesn't make it into Elixir 1.x, it will surely be part of Elixir 2.0. So you have a chance to save yourself some work in the future by adopting a good habit today :) |
👍 for @alco is saying. That goes along the lines of what I was trying to get across in the second half of my long comment above. |
Does this also apply to macros? test("""
Long test desc here
""') do
# ...
end
test """
Long test desc here
""" do
# ...
end |
@lpil I don't think so, because the proposed warning would only be given for 0-arity functions. |
Oh, right, of course. :) |
+1 for a warning |
+1 for warning, fyi: Credo already has a warning for this |
I see how this could help, but I also prefer keeping parens off zero arty calls. I wonder if instead of warning when parens are missing, the warning would come up when there is a local variable name and a function with zero arity in the same scope. Example: conn = conn
conn = get(conn, ...etc.) In this case the call to That may not prevent all the bugs, but might at least prevent some of the more confusing situations? |
I am strongly in favour of this warning because it makes Elixir code so much easier to read. Imagine you have this code: def foo() do
# 50 lines of code
my_function(something)
# ...
end Is That said, @paulcsmith Elixir's issue tracker is reserved for bugs so maybe we can move this discussion to the Elixir mailing list or to elixirforum? 😃 |
@whatyouhide Sure! I thought I saw a discussion label on this issue so I posted here. It seems most people are strongly in favor of adding the parens though so it probably isn't worth. Just wanted to voice an opinion in case it resonated with anyone :) |
The call to conn() is never ambiguous. Only "conn". :) |
@josevalim Yeah that's a good point! Thanks for responding |
I'd prefer to have a warning if there is a function contains more than 50 lines ))) |
@romul if you have a comment related to this feature in particular, please add so, otherwise if you want to discuss other improvements and warnings, you should try the elixir-lang-core mailing list. Let's stay on topic please. |
While `mix test` seems to work just fine using Elixir 1.4, it spits out a bunch of warnings for zero-arity function calls (cf. elixir-lang/elixir#3268). This was fixed by #2. Those changes are still compatible with Elixir 1.3, so no need for a major version bump. But Elixir 1.4 users probably don't appreciate all these warnings.
As described in elixir-lang/elixir#3268
Today, Elixir will transform
var
intovar()
in casevar
does not exist in the user context. I would like to propose those cases to emit a warning from today on.The downsides is that we add a tiny inconsistency. We can make function call without parens unless it is a function with null-arity. On the other hand, we already have inconsistency in the sense we can only shadow function calls with null-arity by variables. So we are exchanging one by the other.
I have bitten by this issue twice today and I believe a warning to be a far compromise.
The text was updated successfully, but these errors were encountered: