Proposal: Property Accessor Function Shorthand

Isiah Meadows contact at isiahmeadows.com
Tue Dec 3 11:33:13 UTC 2019


BTW, all this is very much just a special case of this (existing stage
1) proposal, and is part of why it exists:
https://github.com/tc39/proposal-partial-application

I do find it surprising that property access isn't addressed there,
but it seems like it was likely just overlooked - it has no mention in
the repo, in the open issues, or even in the closed issues or any of
the open or closed pull requests.

-----

Isiah Meadows
contact at isiahmeadows.com
www.isiahmeadows.com

On Tue, Dec 3, 2019 at 5:43 AM Michael Luder-Rosefield
<rosyatrandom at gmail.com> wrote:
>
> At the cost of adding more code, but giving more power, perhaps what we want is something akin to Kotlin's `it` keyword:
> https://kotlinlang.org/docs/reference/lambdas.html?_ga=2.238822404.500195435.1575368476-1345353619.1575368476#it-implicit-name-of-a-single-parameter
>
> it: implicit name of a single parameter
> It's very common that a lambda expression has only one parameter.
> If the compiler can figure the signature out itself, it is allowed not to declare the only parameter and omit ->. The parameter will be implicitly declared under the name it:
> ints.filter { it > 0 } // this literal is of type '(it: Int) -> Boolean'
>
>
> What we'd want is something concise and non-ambiguous to fulfill the same role; something that cannot currently be a valid identifier, maybe. This is the point where I start scanning the keyboard for underutilised symbols... I'm thinking the hash symbol would work. To re-use the original example:
>
> ```js
> const activeProducts = products.filter(#.active);
> const productNames = products.map(#.name);
> const sortedProducts = _.sortBy(products, #.name);
> const { true: activeProducts, false: inactiveProducts } = _.groupBy(products, #.active);
> ```
>
> It makes intuitive sense in 2 ways, I think; # makes you think of the object hash you're extracting a property from, and also is familiar as something's id from CSS selectors.
>
> We could also extend it to represent multiple parameters: # is also aliased as #0, the 2nd parameter is #1, etc.
>
> Further, dynamic properties would work too: `const fooProducts = products.filter(#[foo]);
> --------------------------
> Dammit babies, you've got to be kind.
>
>
> On Mon, 2 Dec 2019 at 22:32, Waldemar Horwat <waldemar at google.com> wrote:
>>
>> On 11/24/19 9:17 PM, Bob Myers wrote:
>> > FWIW, the syntax `.propName` does appear to be syntactically unambiguous.
>>
>> It conflicts with contextual keywords such as `new . target`.
>>
>>      Waldemar
>> _______________________________________________
>> 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


More information about the es-discuss mailing list