Syntax sugar for default arguments

classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Syntax sugar for default arguments

Enrico Colombini
On 25/04/2013 17.19, Andrew Starks wrote:
> This is my most hated bug. I feel like I can probably find this bug in just
> about every piece of lua code, even though finding it doesn't mean that the
> expected arguments wouldn't be processed correctly. That is, usually the
> caller is expected to pass a non-bool or nothing, and false would act as a
> lot like it's supposed to, but not always.

Well, I don't consider it a bug... but a slight annoyance, yes.
My preferred approach is just to avoid default behaviour for booleans:
it's faster and safer for users (including myself).

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: Syntax sugar for default arguments

Javier Guerra Giraldez
On Thu, Apr 25, 2013 at 11:23 AM, Enrico Colombini <[hidden email]> wrote:
> My preferred approach is just to avoid default behaviour for booleans: it's
> faster and safer for users (including myself).


or make false the sane default.  in most cases is just a matter of
carefully choosing argument names.

--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: Syntax sugar for default arguments

Andrew Starks
In reply to this post by Dirk Laurie-2
I see that and how it's different. 

It's the same syntax, and you're suggesting that it be added to the existing function declarations.

I'm suggesting that there is a language symmetry (and simplicity) to be gained by limiting the addition to only named arguments:

myfunc{my_arg = "hello"}

and 

local myfunc = function{my_arg = "howdy", arg2 = "good bye"}
  ---do stuff
end

..just seems like it should be true.

Perhaps it's simpler to apply to every function declaration, as you're suggesting. I think it may be the exact same syntax, other than that.


On Thu, Apr 25, 2013 at 10:46 AM, Dirk Laurie <[hidden email]> wrote:
2013/4/25 Andrew Starks <[hidden email]>:

> a = function(arg1 = 3, arg2 = {"first value", x = d, } or old_value or
> error("arg2 is required"))

Just to clarify: my original intention was not to provide keyword arguments,
but to provide compile-time default values. For example:

function claim(condition,message="Warning",log=io.stderr)
   if not condition then log:write(where(2),message,'\n') end
end

which might be called as

claim(x>0)
claim(x>0,"This will only work if x>0")
claim(x>0,nil,mylogfile)
claim(nil,"Header for default error log")

And I have by now got an answer: people prefer `=` over `or`.


12