rtm / js-pick-notation

Proposal for Picked Properties in JavaScript
MIT License
26 stars 0 forks source link

Did this proposal make any progress? #3

Closed pldilley closed 5 years ago

pldilley commented 5 years ago

Hi Bob Myers,

I very much like this proposal, and am somewhat surprised it hasn't gained more traction. At first I thought this solved it, but no.

From my understanding the current proposal is this:

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

Two questions:

  1. How would/could destructed arrays be included? Does it need to be included?

  2. Would this create a new object on the fly? Would it merge properties if using an existing object? How would that latter syntax look?

    const a, b, params = { z: 10 };
    params.{ a, b } = { a: 1, b: 2, c: 3 };
    console.log(params);  // { a: 1, b: 2, z: 10 }
rtm commented 5 years ago

Thanks for your interest. I'm guessing that this will get picked up by some well-known person who will put his or her own spin on it, and have the ability to push it forward.

To answer your questions:

  1. params.{a, b} = ... does not create a new object any more than params.a would.
  2. Although there are semi-obvious ways to extend this to arrays, arrays are really different animals, and the use cases are different, and there is a notion of "ranges" and "slices", and I thought the proposal would have a better chance of gaining traction if the initial version did not deal with arrays.

On Tue, Feb 26, 2019 at 10:50 PM Paul Dilley notifications@github.com wrote:

Hi Bob Myers,

I very much like this proposal, and am somewhat surprised it hasn't gained more traction. At first I thought this https://github.com/zkat/proposal-as-patterns solved it, but no.

From my understanding the current proposal is this:

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

Two questions:

1.

How would/could destructed arrays be included? Does it need to be included? 2.

Would this create a new object on the fly? Would it merge properties if using an existing object? How would that latter syntax look?

const a, b, params = { z: 10 }; params.{ a, b } = { a: 1, b: 2, c: 3 }; console.log(params); // { a: 1, b: 2, z: 10 }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rtm/js-pick-notation/issues/3, or mute the thread https://github.com/notifications/unsubscribe-auth/AABfR3JVvHl4DA-uwkxjlN7Uri7oIudwks5vRiqqgaJpZM4bT0zq .

pldilley commented 5 years ago

@rtm Thank you for your response!

Perhaps having a working Babel plugin would persuade more people, and also allow testing of the theory? I would love to help!

Just some follow up Q's:

  1. const target.{ a, b } = { a: 1, b: 2, c: 3 }; - So const would just define a and b, but not target? Or would it be disallowed in this instance (requiring let for all on a line above)

  2. I agree with you, it don't really see it being as useful for arrays.

It's tricky because square bracket notation is used for dynamic read/write, so it wouldn't be easy to tell the difference for a single item only. I'm wondering do you have any ideas in mind for the future?

let target, a, b, c;

// Does this mean "target[0] = 1", or "target[undefined] = [1, 2, 3, 4]" ?
target[a] = [1, 2, 3, 4];

// context is clear here, it can only mean a single thing - but it still looks ehhh
target[a, b, c] = [1, 2, 3, 4];

I noticed for option chaining they introduced a dot to deal with this situation: obj?.[expr] Could do the same? target.[a] = ['1', '2', '3', '4']; // target[0] ~> '1'

rtm commented 5 years ago

Not exactly. The easiest way to think of this is as a.{p, q} being an almost-exact analog to a.p, but evaluates to an object containing the p and q properties of a. On the left hand side of an assignment, a.{p,q} = obj;, is very close to a.p = val, except that instead of val being assigned to the p property of a, the p and q properties from obj as assigned to the corresponding properties of a (which already exists). You are not declaring anything here; you are either referencing, or assigning.

To create a new object with a couple of fields from an existing object, the relevatn syntax would bve

const newObject = oldObject.{p, q};
pldilley commented 5 years ago

Thank you again for the responses. Last thing: since it's basically doing destructuring in reverse, rather than call it picking, one could perhaps call it "restructuring". Maybe more obvious naming?Just a thought.

pldilley commented 5 years ago

P.S. That's it from me! Closing