LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

Array slice with a static range of length 1 doesn't yield an array

Open ymeine opened this issue 9 years ago • 7 comments
trafficstars

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.

ymeine avatar Jun 15 '16 09:06 ymeine

this is similar to the array gotcha:

my-array =
  * 'lala'
console.log typeof my-array # => string

both of these should be removed

heavyk avatar Jun 15 '16 12:06 heavyk

@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.

summivox avatar Jun 17 '16 17:06 summivox

@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.

summivox avatar Jun 17 '16 18:06 summivox

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.

vendethiel avatar Jun 17 '16 18:06 vendethiel

I'm a bit guilty as well, since I wrote some of the "unrolling" code of ranges in the lexer. Sorry.

vendethiel avatar Jun 17 '16 18:06 vendethiel

@vendethiel : No problem. Didn't know you stopped. Do you have time for a short private talk?

summivox avatar Jun 17 '16 18:06 summivox

Not quite right now. But I'll try to be over at IRC during the weekend, or maybe gitter if you prefer it.

vendethiel avatar Jun 17 '16 19:06 vendethiel