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

Getting the whole assigned result #9

Open
micnic opened this issue Jun 24, 2018 · 16 comments
Open

Getting the whole assigned result #9

micnic opened this issue Jun 24, 2018 · 16 comments

Comments

@micnic
Copy link

micnic commented Jun 24, 2018

As I see the examples for this proposals mainly cover the nested object properties / array elements value accessing, but can it be used for getting the whole value that is assigned?

Is the following behavior valid in the context of this proposal?

const { y } as x = { y: 1 };

// y => 1
// x => { y: 1 }
@getify
Copy link

getify commented Feb 14, 2019

FYI, I think this use-case can already be handled decently with:

let x, { y } = x = { y: 1 };

Unfortunately, with const, it's a little uglier when twisted around, but still reasonable:

const x = { y: 1 }, { y } = x;

@pldilley
Copy link

pldilley commented Feb 26, 2019

@getify - I don't think this is what @micnic wanted though.
For example: let x, { y } = x = { y: 1, g: 2 }; // x ~> { y: 1, g: 2 }

The use case I'm thinking of is something like this:

const { only, what, I, need, thank, you } as params = veryLargeObjectWithLotsOfRubbish;
console.log(params);   // { only, what, I, need, thank, you }

@ljharb
Copy link

ljharb commented Feb 26, 2019

@pldilley the intention as i see it of this proposal is very much not to provide that, but instead to provide the full object as the as, and also create the bindings (6 in total). If it worked like you suggested, then that sample would only create 1 binding.

@pldilley
Copy link

Please can you give me an example?

From what I understand, it's this:

const test = { a: 1, b: 2, c: 3 };
const { a, b } as x = test;
console.log(x); // ---> x is { a: 1, b: 2 }

Wouldn't make sense for x to include c. That would mean x and test are just the same - something we already have!

@ljharb
Copy link

ljharb commented Feb 26, 2019

No, x would === test.

It's not that useful for variable destructuring of the top level - it's much more useful for destructured function arguments, and nested destructured arguments.

@pldilley
Copy link

pldilley commented Feb 26, 2019

Ah I see... Well I have to admit I'm not so sure that's useful...

For example:
{ a: 1, b: { c: 3 } }`

Why not just do (less characters too):

const { a, b } = { a: 1, b: { c: 3 } };
console.log(b);
console.log(b?.c);

@pldilley
Copy link

I would be interested to know what @micnic and @zkat intended? Does it "restructure" the destructuring or does it get a reference to the parent when destructuring children?

@ljharb
Copy link

ljharb commented Feb 26, 2019

Because then, to also create a c binding, you have to do a second destructuring line.

@pldilley
Copy link

image

@pldilley
Copy link

Apologies for the repeat postings. There's a strawman proposal for what I (and micnic?) was hoping for: https://github.com/rtm/js-pick-notation

@micnic
Copy link
Author

micnic commented Feb 27, 2019

@pldilley actually, when I originally asked this question did not think about the "restructure", I was concerned only about the creation of an object and in the same time to get a reference to it and it's members. Only after you proposed to "restructure" the object I was thinking that it's a good idea, but still, I think we should not go too far from "import as" and "export as" semantics.

@pldilley
Copy link

Thank you for your responses to both of you :) I agree with what has been said about as.

@heavyk
Copy link

heavyk commented Apr 19, 2020

I would like to see this syntax for precisely the use-case of function arguments:

const fn = ({h} as ctx, [first] as arr) => {
  // ...
}

I posted about this on discourse and got pointed to this proposal: https://es.discourse.group/t/destructure-an-argument-and-retain-a-reference-to-the-original-argument/311/2

I really like this proposal 👍 it's even better and more expansive than I the simple idea I had.


wait, upon reading more closely, I want a reference to the original object/array, not to a restructured version of it with only the single parameter I needed out of it. my use case is I often times pass around an object between functions, like a context or language texts which I need to pick only certain ones out of the object, and I want to pass the original object on in it's entirety to the next function. eg.

const render_something = ({h} as ctx, ...args) => {
  return h('div',
    // stuff
    render_something_else(ctx)
  )
}

@getify
Copy link

getify commented Apr 19, 2020

I want a reference to the original object/array, not to a restructured version of it with only the single parameter I needed out of it

I don't think it makes sense at all for { h } as ctx to not have the re-structured subset object as the ctx... to have it instead be the underlying original argument and not related in any way to the destructure pattern you see immediately to the left of it? That's super confusing and opposite of my expected semantics for a word like "as".

@getify
Copy link

getify commented Apr 21, 2020

Thinking about this further, I could imagine flipping it around to:

function renderSomething(ctx as { h }) {
   // ..
}

In that order, it makes a little more sense to me that ctx points at the argument, and then { h } destructuring is applied. It's still possibly confusing since it kinda implies that ctx is as the destructuring pattern. But I could at least see it remotely working in that order. But definitely not { h } as ctx.

Maybe we could instead do:

function renderSomething(ctx with { h }) {
   // ..
}

@heavyk
Copy link

heavyk commented Apr 21, 2020

I like both of those ideas

my old-school js knowledge tells me to never use the with keyword, ever :)

the ctx as { h } is probably the most clear for me, and, even though it could be slightly confusing to have both as-before and as-after versions, it opens up the ability to write something like:

const orig_obj = { lala: 1234, not_included: 'below' }
const obj = orig_obj as { lala }
// which is essentially the same as:
const orig_obj = { lala: 1234, not_included: 'below' }
const { lala } = orig_obj
const obj = { lala }

which I think is quite cool! great suggestion!!

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

No branches or pull requests

5 participants