Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 6365 - Multiple var declaration (AutoTupleDeclaration) #341

Closed
wants to merge 23 commits into from

Conversation

9rnsr
Copy link
Contributor

@9rnsr 9rnsr commented Aug 27, 2011

http://d.puremagic.com/issues/show_bug.cgi?id=6365

Based on #321, declare multiple variables at once, and initialize them in the corresponding tuple fields.

Syntax:

TupleDeclaration:
    StorageClasses ( TupleTypeList ) = Initializer ;
    ( TupleTypeList ) = Initializer ;

TupleTypeList:
    TupleType
    TupleType , TupleTypeList

TupleType:
    StorageClasses BasicType Declarator
    BasicType Declarator
    StorageClasses Identifier
    Identifier

TupleTypeList is similar to ForeachTypeList.

If you want to write left parenthesis at beginning, you cannot omit the type or storage class of first variable.

(x, y) = getCoordinate();  // NG, this is not multiple var declaration
(auto x, y) = getCoordinate();  // OK
(double x, y) = getCoordinate();  // OK, types are specified explicitly

@dsimcha
Copy link
Contributor

dsimcha commented Aug 27, 2011

What about functions that return multiple values of different types? Would this work?

(double x, int y) = fun();

@9rnsr
Copy link
Contributor Author

9rnsr commented Aug 27, 2011

Yes. In your case, fun() should return tuple type, and

(double x, int y) = fun();

is just translated to

auto __tup = fun();
double x = __tup[0];  // __tup[0] has a type that is implicitly convertible to double
int y = __tup[1];     // ditto

Therefore, fun() can return Tuple!(int, int) or Tuple!(double, int).

@dsimcha
Copy link
Contributor

dsimcha commented Nov 14, 2011

I love this idea. Is there any language design-level reason why it shouldn't be included or are we just waiting for Walter to get around to merging it?

@9rnsr 9rnsr closed this Nov 15, 2011
@9rnsr 9rnsr reopened this Nov 16, 2011
@nazriel
Copy link
Contributor

nazriel commented Jul 19, 2012

Bump, this is really sexy.
Same question as @dsimcha : Is there any language design-level reason why it shouldn't be included?

@dsimcha
Copy link
Contributor

dsimcha commented Jul 19, 2012

On looking at this again one concern I see is the implicit temporary. This is caused by creating __tup and would cause postblits to run an extra time in cases where structs are being copied. This may not matter, though, since we're trying to discourage designs where structs can be arbitrarily expensive to copy.

@Dav1dde
Copy link

Dav1dde commented Sep 9, 2012

Bump, any news on this?

@nazriel
Copy link
Contributor

nazriel commented Sep 9, 2012

@WalterBright what ya think about this?
@9rnsr has done amazing job to make work with Tuples great experience!

@dcousens
Copy link

dcousens commented Sep 9, 2012

Bump, really looking forward to seeing this merged in. The simplicity of tuples is not going away :).

@jpraczyk
Copy link

Bump, bump, bump. Do want!

@robik
Copy link

robik commented Sep 12, 2012

This is just awesome.

I have a question, does it work on function declarations? Like public (int, int) getCoordinates() ?

@nazriel
Copy link
Contributor

nazriel commented Sep 12, 2012

@robik I don't think so, but probably @9rnsr will know better

@9rnsr
Copy link
Contributor Author

9rnsr commented Sep 13, 2012

@robik Unfortunately, no. This enhancement just supports distributing tuple fields to each variables in initializing.
I think your implication is "returning multiple values from function without packing (like Tuple!(int, int))", and it is completely different thing. And it's hard to implement, because it requires ABI change.

@repeatedly
Copy link
Member

LGTM 👍

@meh
Copy link

meh commented Sep 24, 2012

+1.

@9rnsr
Copy link
Contributor Author

9rnsr commented Sep 24, 2012

Documentation for this enhancement: dlang/dlang.org#160

@andralex
Copy link
Member

Just to give a bit of feedback - we're looking into this. If we conclude that this language addition solves most of the issues with tuples (so no radical redesign is needed), we'll pull this in. We don't want to pull this thinking that it might hurt a better future design.

@9rnsr
Copy link
Contributor Author

9rnsr commented Sep 24, 2012

We don't want to pull this thinking that it might hurt a better future design.

What might be hurt in the future? Syntax? Semantics? or ABI issue?
I'd like to know that you worried in current.

@andralex
Copy link
Member

Syntax and semantics mostly. Walter and I are worried about overarching design more than anything. Consider that the "perfect" tuples for D are six good design decisions away. This pull request deals with destructuring and makes one decision, which is shaping all future decisions. If we later conclude tuples need some additional form of destructuring, or something different entirely, we'll have to add that too. And then some other stuff on top of that. So instead of making one move at a time and then analyzing the result, we want to think the strategy forward so as to have a good overal design, not (only) a good design for destructuring.

Now, it's quite possible that destructuring is about all we need, or that whatever else we need doesn't interact badly with destructuring. In that case this request will be pulled, and everybody will be happy.

@9rnsr
Copy link
Contributor Author

9rnsr commented Sep 24, 2012

Thanks for your answer, @andralex . I cannot reply to your worry immediately, but I also would like to think about it.

@ghost
Copy link

ghost commented Sep 24, 2012

I'd like to be able to ignore a return value if that's possible. For example

(int a, void) = fun() -> discards second value

That way I don't have to forcefully introduce new symbol names into the current scope (using _ is a nogo because there might already be such a symbol, e.g. in foreach loops or a previous tuple destruction).

braddr pushed a commit to braddr/dmd that referenced this pull request Oct 22, 2012
Size optimisation of std.datetime (saves 12KB)
@WalterBright
Copy link
Member

I agree with Andrei, while I don't see anything obviously wrong with this proposal, I'd like to see a complete tuple design before committing ourselves to one aspect of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.