LiveScript
LiveScript copied to clipboard
Array slice with a static range of length 1 doesn't yield an array
Description
When doing some heavy processing on lists of objects, I usually write my code as it would finally be, but first want to test it for only one item. Therefore, I usually add a simple slice accessor behind the array to shorten it. However, I noticed that for a static range of length one, the result is not an array but the value at the unique index contained in the range.
This breaks code relying on arrays only.
Examples
Actual (failing)
['a' 'b' 'c'][1 to 1] # => 'b'
['a' 'b' 'c'][1 til 2] # => 'b'
# while ranges are arrays
[1 to 1] # => [1]
[1 til 2] # => [1]
Expected
['a' 'b' 'c'][1 to 1] # => ['b']
['a' 'b' 'c'][1 til 2] # => ['b']
Workaround
start = 1
end = 1
['a' 'b' 'c'][start to end] # => ['b']
end = 2
['a' 'b' 'c'][start til end] # => ['b']
Context
I tested with LiveScript 1.5 and 1.4, both have the issue.
this is similar to the array gotcha:
my-array =
* 'lala'
console.log typeof my-array # => string
both of these should be removed
@heavyk what you mentioned is probably the number 1 nasty surprise of LiveScript, but rest assured this is the intended semantics. OTOH what OP posted I agree should be fixed.
@vendethiel : I just looked at the source. Not surprisingly it is caused by our hacky lexer-parser division on loopheads and (again not surprisingly) there is not a easy local fix. To be honest I like current semantics; it's just the impl has grown beyond reason and has caused multiple outstanding issues with no easy/local/logical fixes.
While the original plan is to address this with LS/next, I have changed my mind. It needs fixing now. I will open a separate issue.
No point paging me anymore -- I'm not working/following on LS anymore, as I stated in my other message (because it seems LS.next isn't gonna happen). Best of luck with the hacks piled on hacks. FWIW, there's another issue for string vs array slicing for literal ranges. If you need me to, I could find it.
I'm a bit guilty as well, since I wrote some of the "unrolling" code of ranges in the lexer. Sorry.
@vendethiel : No problem. Didn't know you stopped. Do you have time for a short private talk?
Not quite right now. But I'll try to be over at IRC during the weekend, or maybe gitter if you prefer it.