[erlang-questions] simplified attribute declaration
Kostis Sagonas
kostis@REDACTED
Fri Mar 19 13:23:55 CET 2010
Kenneth Lundin wrote:
> Hi,
>
> The possibility to specify attributes without parentheses is a mistake
> that we will corrected as soon as
> possible. It was introduced together with the -spec and -type notation
> which does not need parentheses.
>
> I don't understand why it is cool.
> Don't even try to use this syntax in your code you will be punished
> with syntax error as soon as we fix this bug.
>
> /Kenneth Erlang/OTP, Ericsson
>
>
> On Thu, Mar 18, 2010 at 10:14 AM, Vlad Dumitrescu <vladdu55@REDACTED> wrote:
>> Hi,
>>
>> I don't remember anyone mentioning this before, but I think it's cool
>> and should be promoted as the recommended syntax:
>>
>> All attributes can now (R13) be specified without parantheses!
>>
>> -module hello_world.
>> -export [init/1, stop/2].
>> -define DEBUG(X), io:format("debug: ~p~n", [X]).
Let me try to clarify the issue of having the possibility to write
attributes without parentheses because I see a lot of confusion (perhaps
naturally so, as this feature is not documented), first in Vlad's mail,
then in the various replies that his mail has generated -- which has
provoked Kenneth's "threatening" mail to the mailing list.
What is really happening is that attributes which take just one argument
do not need to be enclosed in parentheses. On the other hand, attributes
with more than one argument (read: whenever there is a ',' involved, as
in the case of -define above) need to be enclosed in parentheses. This
was introduced in order to allow types and specs to be written without
parentheses but the mechanism is more general and applies to all 1-arity
attributes. This is fine, I think, and does not need to be changed.
It is true that users who have spent years getting used to the "Erlang
way of doing things" might find this possibility confusing at first. On
the other hand, those that have some exposure to ML or Haskell will
recognize something familiar to them: think of all attributes as taking
only _one_ parameter and the parentheses are needed for grouping what's
inside this parameter. In other words, read the three attributes above
as written:
-module hello_world .
-export [init/1, stop/2] .
-define (DEBUG(X), io:format("debug: ~p~n", [X])) .
So, in the last case, it's really the ',' that forces the parentheses.
Personally, I do not see any compelling reason why one should be
REQUIRED to put parentheses around the single argument of unary
attributes. Of course, note that even today this is entirely optional:
one CAN put parentheses around this argument if one really wishes to, in
the same sense that one can write 40 + 2, (40 + 2), (40 + (2)), etc.
Kostis
More information about the erlang-questions
mailing list