lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Though question, 
The 1. version
  function f(x,y=0)
has the advantage that it shows the assignment or initialization of y with 0, but it
does not show, that this only happens if y == nil, the condition part is not shown. It also has the advantage that is close to other languages.

the 2.
function f(x,y or 0)
Shows that there is a condition but not the initialization. It is similar to how Lua handles this in other situations.

Maybe it would be better if there was syntactic sugar for conditional assignments and use this syntax then here as well:

y or = 0 
  -> if not y then y = 0 end
t.x or = "hello"
  -> if not t.x then t.x = "hello" end
This is different from y = y or 0 as the assignment  in here always happens, it's just you end up with the same value in y if y ~= nil.
In y or = 0, the assignment is really only done if y is nil.

Now the 2nd step would be to allow this syntax in the function declaration.
function f(x, y or = 0)

Now I don't think "or = " is very beautiful, but I don't have a nicer idea at the moment, and it shows both, the condition and the assignment.


-----Original Message----- 
> From: "Dirk Laurie" <> 
> To: "Lua mailing list" <> 
> Date: 21-04-2013 08:43 
> Subject: Syntax sugar for default arguments 
> function f(x,y=0)
>     function f(x,y or 0)
> to mean
>     function f(x,y) y=y or 0
> Neither would break existing code.
> Which of the above should I choose to implement as a patch?