tc39 / proposal-slice-notation

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

Would it accept any expressions like [0:foo] for example? #26

Closed caub closed 6 years ago

caub commented 6 years ago

Would it throw a SyntaxError for dynamic code like:

const a = [1,2,3,4,5,6];
const x = 3;
a[0:x] // possibly using a[0:[x]] syntax, similarly to object initializers
gsathya commented 6 years ago

From the FAQ:

Can the upper bound, lower bound or the step argument be an arbitrary Expression? Currently the proposal (arbitrarily) restricts them to be an IdentifierReference or DecimalDigits.

caub commented 5 years ago

Just to clarify

const a=[1,6,5,7,4,1,2,3,2,1,5,8];

const n = a.length/3; a[0:n] // valid (IdentifierReference)
a[0:(a.length/3)] // not valid

const len = a.length; [...0:len:3].map(i => a[i:i+3]) // valid (IdentifierReference)
[...0:a.length:3].map(i => a[i:i+3]) // not valid

Not allowing expression in the range literal form used for creating ranges [...a:b] would not be convenient at all

https://github.com/tc39/proposal-slice-notation/issues/19#issuecomment-427798886