lua-users home
lua-l archive

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


Sean Conner <sean@conman.org> wrote:
> Benoît de Chezelles <benoit.dechezelles@gmail.com> wrote:
>> 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?

Thanks for sharing that link. That system of defining both positional and
named parameters, both optional, and both with vargs is quite interesting.

I am developing my own fork of Lua and how these definitions work would fit
very well into that design. Thanks for bringing this to my attention!


> 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 

Assuming the definition above:

> foo(1,2,3,4,5,6)
Error, too many fixed arguments.

> foo(x = 1 , y = 2 , z = 3 , a = 4 , b = 5 , c = 6)
Error, no named argument 'x'.

> foo(*{1,2,3,4,5,6}) # the "splat" operator I guess

The splat symbol isn't an operator, it is a variable qualifier. If there is
one preceding splat to the variable name it will be an array containing all
the positional parameters not specifically named. If there are two splat
symbols the variable will be a hash containing undefined named parameters.

If there is only a splat symbol and no name it means no optional positional
or named parameters may be passed with two splats. The splat symbol is only
a qualifier for variables in a function definition.

As an example if we redefine 'foo' as:

foo(x, y, z, *more, a = 1, b = 2, c = 3)

foo(1,2,3,4,5,6)
x=1, y=2, z=3, more=[4,5,6]

foo(a = 2)
error: missing positional arguments

foo(1,2,3, a = 2, z = 4)
error: unknown parameter 'z'


If we redefine foo again as:

foo(*, a = 1, b = 2, c = 3, **more)

Now that there is only a single splat operator NO positional arguments will
be allowed, only named ones. Any not listed named parameters that are passed
will be placed in the 'more' hash.

foo(1,2,3, a=4);
error: positional arguments disallowed.

foo(a=1, b=2, c=3, d=4, e=5, f=6)
a=1, b=2, c=3, more={ d=4, e=5, f=6 }


I guess a function that took absolutely zero params would be 'foo(*,**)'!

In my implementation I would just assume the splat or doublesplat effect was
enabled, that is no optional parameters unless specifically declared.

The nice thing is with my Index data type I sort of get Tuples and NamedTuples
for free, which is really nice for implementing optional parameters.

Thanks again for that link, that really works well for me!

~Paige