lua-users home
lua-l archive

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


It was thus said that the Great Benoît de Chezelles once stated:
> 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!

  Which reminds me, as long as the Lua code hasn't been stripped. Lua 5.3
debug.getlocal() can return the names of the function parameters:

	local function parameters(f)
	  local function n(s,v)
	    v = v + 1
	    local name = debug.getlocal(s,v)
	    if name then
	      return v,name
	    end
	  end
	
	  return n,f,0
	end

	function print_args(f)
	  for i,arg in parameters(f) do
	    print(i,arg)
	  end
	  print()
	end

  So there's that ... 

  Anyway ... 

> > >  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})

  You can drop the parenthesis in this case:

	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?

  I don't know.  I read the page, and it seems overly complex to me.  I
would have constructed it like:

	def foo(x,y,z,a = 1 , b = 2 , c = 3)
	end

  Calling foo() would be 

	foo(1,2,3,4,5,6)
	foo(x = 1 , y = 2 , z = 3 , a = 4 , b = 5 , c = 6)
	foo(*{1,2,3,4,5,6}) # the "splat" operator I guess
	foo(**{"x",5,"y",6,"z",7}) # assuming I got the "splatsplat" operator right

  Instead of complicating the definition, make it so you can do whatever you
want.  I mean, you could mix things up:

	foo(1,2,3,a = 4 , b = 5 , c = 6)
	foo(*{1,2,3},4,5,6)
	foo(1,2,**{"z",5,"b",7})

  But that's me.

  -spc (Also, I don't care for the ':' assignment thing but I could live
	with it ... )