Try this code:
a = 1
def a()
2
end
puts a # 1
puts a() # 2
I think this is correct, for a is a value, and a() is a function.
There are many programming languages not support this feature, so I'm very happy when I saw it.
But, try this code:
def a()
2
end
puts a # output: 2
It shouldn't be compiled, for a is a value, not a function or closure. This can lead to syntax inconsistencies and confusion.
And, try this code:
puts a() # output 3
def a()
2
end
def a()
3
end
puts a() # output 3
It shouldn't be compiled too, for the two functions are not overloaded relationships: they have the same parameter and return type.
You might be thinking as a Pythonist, were you a Rubyist, that would make sense for you.
Parentheses are optional for method calls in Crystal. So, in the first two examples, when you have a identifier that could be a local variable or a method call, the local variable is preferred.
Then, the syntax of a() is unambiguously a method call.
In the last example, this is a feature of the language to be able to override, or re-define methods by defining one with the same name and signature. See the reference.
This is all by design and I'm afraid we're past a stage where we would change the language so fundamentally :)
To clarify:
It shouldn't be compiled, for
ais a value, not a function or closure. This can lead to syntax inconsistencies and confusion.
In Crystal, a is either a reference to a variable a or a call to method a (in case no such variable exists in the local scope). That might look concerning, but in practice it's actually not a relevant issue. Neither in Ruby nor Crystal.
And I think this kind of makes sense. There is really not much difference in whether you're referring to a local variable or, for example a property of an object. Both can be accessed by the same syntax. And properties are just methods, which is a very simple concept.
If you have any more questions on how/why the language works, then I suggest you head over to https://forum.crystal-lang.org/. We'll be happy to explain the design decisions!
Most helpful comment
This is all by design and I'm afraid we're past a stage where we would change the language so fundamentally :)