Tuples a first class feature, manu's new unary operator and named arguments

Timon Gehr timon.gehr at gmx.ch
Sun May 10 17:47:12 UTC 2020


On 10.05.20 09:00, Panke wrote:
> On Sunday, 10 May 2020 at 03:14:08 UTC, Timon Gehr wrote:
>> I don't see any reason why the concept "multiple function arguments" 
>> should be separate from the concept "multiple tuple elements". It's a 
>> distinction without a difference and I think it is a design mistake.
> 
> But that would mean we need a way to prevent a tuple from unpacking 
> automatically, wouldn't it?
> ...

It does not mean that, because there would be no "unpacking".

The way to think about it is that every function has precisely one argument:

def f(t):
     return t[0] + t[1]

Then there is syntactic sugar. The following definition

def f(a, b){
     return a + b

Would actually mean:

def f(t):
     (a, b) = t
     return a + b

I.e., multiple function arguments are a pattern to which you match a 
tuple. You can then call the function with a tuple:

t = (1, 2)
f(t)

f(1, 2) # means the same as f((1, 2))
         # just like f(t) is the same as f((t))
         # additional parentheses mean nothing

In the grammar, a function call would be a function followed by a 
standard parenthesized expression. You literally parse the same thing 
after a function call as when you encounter parentheses in an expression.

> Or do we only allow exactly one or no tuple in an argument list?
> 
> 

You would be able to match as many tuples as you like, e.g.:

def f(a, (b, c)):
     return a + b + c

print(f(1, (2, 3))) # 6

A parameter list is just a pattern to match an argument to.


More information about the Digitalmars-d mailing list