Proposal: Object/?? Destruction

Steven Schveighoffer schveiguy at yahoo.com
Sun Oct 8 23:20:28 UTC 2017


On 10/7/17 8:56 PM, Timon Gehr wrote:
> On 06.10.2017 23:34, Steven Schveighoffer wrote:
>>>>
>>>
>>> No. All functions take one argument and produce one result. (The 
>>> argument and the result may or may not be a tuple, but there is no 
>>> essential difference between the two cases.) You can match a value 
>>> against a pattern on the function call.
>>
>> It is weird to me that a function with 2 parameters is the same as a 
>> function that takes a 2-element tuple, but a function with one 
>> parameter is not the same as a function that takes a 1-element tuple. 
>> That is where I feel it's a contradiction.
>> ...
> If a function with 2 parameters was the same as a function that takes a 
> 2-element tuple, and a function with one parameter that is a 2-element 
> tuple is the same as a function that takes a 1-element tuple, then a 
> function that takes a 2-element tuple is the same as a function that 
> takes a 1-element tuple. So I think the opposite is the case.
> 
> // those two are the same
> void foo(int a,string b); // match two-element tuple
> void foo((int,string) x); // take two-element tuple w/o matching
> 
> // those two are the same
> void bar(int a,);   // match one-element tuple
> void bar((int,) x); // take one-element tuple w/o matching
> 
> This is like:
> 
> (int a,string b)=(1,"2"); // match
> // vs
> (int,string) x=(1,"2"); // w/o matching
> 
> and
> 
> (int a,)=(1,); // match
> // vs
> (int,) x=(1,); // w/o matching

My questioning comes with this:

void bar(int a);
void bar((int,) x);

To me, it is confusing or at least puzzling that these two aren't the same.

The first is like a "regular" function that doesn't take a tuple.

The second is a new "tuplized" function that takes a tuple. both take 
one parameter (one version via the regular argument syntax, one via a 
tuplized syntax). Why is it not the same? Clearly a tuple of 1 can bind 
to a single value, just like a tuple of 2 can bind to 2 values.

Currently, I can call this:

foo(T...)(T t) if (T.length == 1) // function that takes a single 
element tuple

like this:

foo(1);

Why is this disallowed in your tuple scheme?

> In case this is not convincing to you: Why does your reasoning apply to 
> arguments but not return values? Why should arguments not behave the 
> same as return values? If it does actually apply to return values: what 
> special syntax would you propose for functions that "return multiple 
> values"? Is it really reasonable to not use tuples for that?

I don't understand the question. I would think single value tuples and 
single values would be pretty much interchangeable. It's up to the user 
of the value whether he wants to look at it as a tuple (which has length 
and must be indexed) vs. a single value.

>> Right, but cases where T is expected to match to exactly one type will 
>> now match with multiple types. It messes up is(typeof(...)) checks.
>>
> 
> All new language features can be detected using is(typeof(...)) this is 
> usually ignored for language evolution. We'd need to check how much code 
> relies on this specific case not compiling.

I definitely don't have an answer off hand, but I wouldn't be surprised 
if this broke at least some code in phobos.

> We can also think about adding a "light" version of tuple support, that 
> just supports unpacking for library-defined tuple types and nothing 
> else, but I'd prefer to have proper tuples.

This flew over my head :)

-Steve


More information about the Digitalmars-d mailing list