-
-
Notifications
You must be signed in to change notification settings - Fork 647
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
Destructuring assignments #4300
Comments
A HUGE +1 on this! Destructuring is so sweet, however I wonder if this syntax is good. We introduce new locals without the |
Instead of this:
You can write this already:
It's not like it's so much worse, is it? I'm just pointing it out, because it currently seems impossible to generally define make supported syntax without presently defined semantics to do something useful. Is that policy open for debate now? Regarding the issue at hand, I've had a hard time adding this to tink_lang, because I found I had two choices, none of which I like:
Let's look at the second example you gave. I suppose you'd write that like so?
Aside from the question whether this is really readable and the issue of exhaustiveness (let's just assume there's a solution), there's another issue: How do you know this is a destructuring assignment? Anything that is an In my attempts I've used If you can get this right, I'm all for it. I just wanted to warn you of all the issues I had trouble solving. Oh and I'm also slightly afraid that this might backfire to fuel the short lambda flame war, but I'll leave the politics to you guys ;) |
Le 08/06/2015 13:18, Dan Korostelev a écrit :
I forget to add the case when you want to reuse already existant var. In my opinion you should let the choice to the user to declare new var or reuse old one. |
I like the original proposal. The assignment makes clear enough that bindings are being introduced, and it's concise. |
By the way i am not using a switch but doing a direct assignment field per field while destructuring, i m doing this early and let the compiler complain if the field did not exist. |
@pleclech can you destructure enums in your implementation? |
Le 08/06/2015 14:37, frabbit a écrit :
|
Le 08/06/2015 14:37, frabbit a écrit :
|
If you restrict this to object literals only, then yes, it is unambiguous. Most of all, the assumption about this being unambiguous is short sighted. It is also the least bit clear how the second example is to work. Through On Mon, Jun 8, 2015 at 1:49 PM, ousado notifications@github.com wrote:
|
While I agree it can be useful in some cases, it does not weight very well in terms of readability and error proneness. It also requires introducing new syntax such as Regarding the problem you're trying to solve, what's wrong with the following: var names = switch (Context.getLocalType()) {
case TInst(c, [TAbstract(a, tl)]): { c : c.toString(), a : a.toString() };
case t: Context.error("Unexpected type", p);
}
//...
trace(names.c)
trace(names.a) I think that's a good enough solution. |
Note that many modern and widely accepted languages have/are getting destrcturing assignments now in one way or another. Just to name a few that come to mind:
It's not like it's something from the "functional world" and it'll be more popular with time thanks to js and its transpilers. |
I like how the debate starts productive and good ideas are laid down and On Tue, Apr 12, 2016 at 11:05 AM, Dan Korostelev notifications@github.com
|
Well, I think Nicolas was talking about keeping the syntax simple and I can agree with this intent. I love the simplicity and even minimalism of Haxe syntax, but pragmatically I often feel the need for tuples+destructuring, so I'm still very much in favor of this feature. |
I bet he is, but we could have discussed how to make this feature in a way On Tue, Apr 12, 2016 at 12:22 PM, Dan Korostelev notifications@github.com
|
I agree, maybe we need to develop some enhancement proposal process, like Python's PEP or something. |
Add lua to the list of multiple-returns/tuple languages. I might add something: Supporting simple multiple returns is more pressing than a full blown destructuring feature imho. It would allow for more natural extern support on the languages that use them in core libraries. It's currently impossible to get Haxe to deal with multivalue return functions in a first class way. You either have to wrap the result somehow (which adds overhead), only type/provide the first argument (which ends up losing info on the other values returned), or use magic methods (and give up communicating anything about the type). I have a branch where I'm poking around on a more elegant solution. The way I have in mind is to add some metadata to a standard typedef: @:MultiReturn typedef Foo = {
a : Int;
b : Int;
}
...
var f : Foo = some_extern() // extern function that returns multiple values 1, 2
trace(f.a); // 1
trace(f.b); // 2 Whenever that type is used in assignment, the compiler detects it and does platform-specific logic for handling multiple returns. local _hx_1, _hx_2 = some_extern()
print(_hx_1)
print(_hx_2) This seems to be straightforward, but the first problem is that anonymous fields (used for typedefs) are a map, so they don't preserve ordering in the compiler metadata that is necessary for preserving the tuple positions. My workaround there was to leverage the cf_pos info to capture the source columns and lines they're declared at, and sort them separately that way when necessary. The second problem is that the MultiReturn variable needs to maintain a local variable alias for each of its fields, which it will use for assignment and access. That's not so bad though, it just needs to be stored somewhere in the data the compiler uses to track variables. So, the steps would include: detect the Also, I suppose rather than using metadata, we could use a special extern class like This is completely half baked at this point, so any thoughts are welcome. |
Since we are now getting short-lambdas soon, could we reopen/reconsider this as well? |
i like destructuring, but this has to go through haxe-evolution with a well thought out proposal which covers all the corner cases and limits of destructuring. |
Let's do it then! @Simn since you originally proposed it, would you be able to do that? As a related question - are there any ways to simulate this via a macro as of now? Are there any existing solutions for that? |
I want to focus on stability for a while so I'm not looking for any new features to implement right now. To be honest I'd be reluctant to vote for my own feature suggestions at the moment. |
In case anyone finds this useful, I wrote a macro as part of the haxe-strings library, which implements assignment destructuring for array-accessible objects, see /vegardit/haxe-strings/blob/master/src/hx/strings/internal/Macros.hx#L143 Macro.unpack([a,b,c] = ["1", "2", "3"]);
trace(a);
// or
var list = ["1", "2", "3"];
Macro.unpack([a,b,c] = list);
trace(a);
// or
Macro.unpack([prefix,value] = "foo:bar".split(":"));
trace(prefix); |
@Simn, almost 4 years have passed since you said that it is not right time to create a proposal and wanted to focus on stability. Is it still not the time? (I created the alternative syntax proposal, but just to start a discussion, i do prefer to have the same syntax as other languages if possible, as described here.) @ncannasse (sorry to bother), if a theoretical proposal (with the design described here) is created and passes an evolution vote will you veto it on grounds of not adhering to your vision for haxe? (As i understand this concern is the only thing that keeps a proposal from being introduced.) |
Could we allow this:
by transforming it to this:
?
This does not require any new syntax and can be detected by checking if the left-hand argument of an assignment binop is something that at the moment gives an invalid assignment error anyway. The implementation itself is straightforward, too.
Use-case: I often have to extract multiple values like so:
Doing that by hand is verbose and error-prone.
The text was updated successfully, but these errors were encountered: