janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.45k stars 223 forks source link

Surprising behavior in {string,tuple,array}/slice #207

Closed andrewchambers closed 4 years ago

andrewchambers commented 4 years ago

In python:

>>> "hello"[0:-1]
'hell'

In janet:

>>> (string/slice "hello" 0 -1)
"hello"
>>> (string/slice "hello" 0 -2)
"hell"

Note if you grep boot.janet for slice, you will see a few instances of -1 used which seem likely bugs.

bakpakin commented 4 years ago

See https://github.com/janet-lang/janet/issues/140. Not a bug, boot.janet is fine.

andrewchambers commented 4 years ago

I see, haha, maybe we gotta add a note to the doc string :P I feel like I wont be the last person

andrewchambers commented 4 years ago

@bakpakin would you consider a negative get? I also note that -0 is technically valid in floating point, though I don't know if it is usable in practice lol.

bakpakin commented 4 years ago

As far as breaking changes go, this one will basically break all code. Also treating -0 differently from 0 is just terrible design. They have the same hash and should be equal.

bakpakin commented 4 years ago

As for negative get, I think that's probably an ok addition (not for in). It would defeat some bounds checking for in, but for get it's probably useful.