Proposal: Selector/Select Expression

Barret Furton barretfurton at gmail.com
Sat Jun 22 17:01:21 UTC 2019


I wonder if this could be accomplished with a proxy object instead of new
syntax.

```js
const o = new Proxy(/* some implementation */);

const pickName = o.name;

pickName({ name: "Bob" }); // "Bob"

// maybe this could work too with a recursive proxy
// with handlers for access and invocation
const safePickBaz = o.foo.bar.baz;

safePickBaz({ foo: 20 }) // undefined
```

On Fri, Jun 21, 2019 at 7:55 PM Bob Myers <rtm at gol.com> wrote:

> Personally, I'm a huge fan of this proposal. It would integrate into the
> language the extremely frequent idiom of defining a pick/pluck like
> function. There is ample prior art in the form of Ramda's pick function
> <https://ramdajs.com/docs/#pick>.as well as `pluck` in RxJS.
>
> In fact, this exact proposal was at one point included in the proposal for
> "extended dot notation", then removed in the interests of making making it
> more digestible.
>
> AFAICS the syntax is completely unambigious:
>
> ```
> const pickName = .name; // equivalent to ({name}) => name
> [{id: 1, name: "Bob"}, {id: 2, name: "Sally"}].map(pickName
> ```
>
> The only objection I can think of is that the leading dot is sometimes
> hard to see. I would have suggested using the hash mark--in fact, an early
> version of the exptended dot notation proposal did propose that--but now
> that has been taken by the private fields proposal, so unless we want to go
> in the direction of keywords or magic operator sequences we are reduced to
> using non-unary operators such as ^, so
>
> ```
> const pickName = ^name;
> ```
>
>
> On Fri, Jun 21, 2019 at 9:47 AM Simon Farrugia <simonfarrugia26 at gmail.com>
> wrote:
>
>> New syntax will always look a bit odd at first until we get used to it.
>>
>> I sometimes find nested arrow functions a bit confusing to understand
>> where one function starts and where it ends particularly when they are one
>> liners.
>> This syntax is more concise and personally I find it more readable.
>>
>> Reasons for the leading dot notation is because I think it's intuitive
>> and also self explanatory:
>> 1) We just remove what is in common on both ends of the expression (like
>> simplifying an algebraic expression).
>> ```
>> const getEmail = user => user.contacts.email;
>> ```
>> 2) The leading dot indicates that the expression reads the property of an
>> object that is not statically defined.
>> ```
>> const getEmail = .contacts.email;
>> ```
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190622/0aa22947/attachment.html>


More information about the es-discuss mailing list