lua-users home
lua-l archive

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


Hello, I just subscribed to the mailing list so I'll try my best to
put my reply in context:
I'm replying to this message:
http://lua-users.org/lists/lua-l/2018-01/msg00330.html

Paige DePol wrote:
> > > I was thinking of adding named parameters, as well as optional parameters,
> > > and have been figuring out the best way to implement them. One issue I am
> > > trying to sort out is an efficient way to create a function signature that
> > > can be quickly calculated at runtime when necessary. Another issue is
> > > determining a nice readable syntax for named parameters!
> >
> >  Um, how about:
> > local addr,err = net.address2(host = 'www.google.com', proto = 'www')
>
> Yes, either that or with colons perhaps?
>
> local addr,err = net.address2(host: 'www.google.com', proto: 'www')
>
> [...]
>
> With named parameters the coder should also be able to provide the parameters
> themselves in any order, so the following would be the same:
>
> local addr,err = net.address2(host: 'www.google.com', proto: 'www')
> local addr,err = net.address2(proto: 'www', host: 'www.google.com')
>
> At least, that is my goal...

Huge supporter of named parameters here :) It would be great for Lua instead
of relying on 'options' table, with manual local var mappings, like:

-----
-- What I use to do to have 'named arguments' in Lua
function func(opt)
  opt = opt or {}
  local arg1 = opt.arg1 or "default1"
  local arg2 = opt.arg2 or "default2"
  -- use arg1 & arg2
end

func({arg1 = 1, arg2 = 2})

-----
-- Better!
function func(arg1 = "default1", arg2 = "default2")
  -- use arg1 & arg2
end

func(arg1: 1, arg2: 2)

About the syntax that it could have, and the features of this kind of function
call, I want to show what the language Crystal (a compiled language)
has come up with, which is to me the best method call syntax and flexibility
and readability possible:

https://crystal-lang.org/docs/syntax_and_semantics/default_values_named_arguments_splats_tuples_and_overloading.html

Crystal uses the colon syntax for named arguments, but also allows you to use
positional arguments, varargs for positional arguments, and varargs for named
arguments. You can also use an external/internal argument name as described at
the bottom of the above link.

I don't think the distinction between varargs for positional vs named argument
makes sense in Lua, but maybe the rest could?

---------------------------------------------------
-- Here are some basic examples:

-----
-- Use of mixed positional/named arguments/varargs
function some_func(arg1, arg2, ...)
  -- arg1 is 1
  -- arg2 is 2
  -- ... is something like the table {3, 4, "arg5" = 5}
end

some_func(1, arg2: 2, 3, 4, arg5: 5)


-----
-- External/internal argument name
function increment(value, by amount)
  return value + amount
end

increment(1, by: 3)

-----
-- Default arguments
function add(x, y, z = 2)
  return x + y + z
end

add(y: 2, 1) -- 5