tc39 / proposal-slice-notation

http://tc39.es/proposal-slice-notation/
MIT License
526 stars 19 forks source link

Expressions as inputs #16

Closed atticoos closed 4 years ago

atticoos commented 6 years ago

The proposal and its examples show static usage

a = [1, 2, 3, 4]
a[1:3]
// → [2, 4]

Eventually the question will be raised if access can be done dynamically by an expression:

Whether it's by variables

i = 1
j = 3

a[i:j]
// → [2, 4]

Or with operators

i = 2
j = 2
a[--i:++j]
// → [2, 4]

Or other expressions

i = () => 1
j = () => 3
a[i():j()]
// → [2, 4]
atticoos commented 6 years ago

I couldn't tell you how most syntactic language features like this work under the hood, so I can't argue for or against this other than "yea, it should, just like it would for other bracket-notation access a[(() => 1)()]".

I imagine it's expected to support dynamic usage like this.

dissimulate commented 6 years ago

This is how it works in Python:

a = 1
b = lambda: 1
c = [1, 2, 3, 4]

c[1:3] # [2, 3]
c[a:3] # [2, 3]
c[b():3] # [2, 3]

Note: the last index is not included in the slice, it's from the first until the second.

leobalter commented 4 years ago

Sounds good to me, my only suggestion here is to use AssignmentExpression instead of Expression.