expr icon indicating copy to clipboard operation
expr copied to clipboard

Unexpected behaviour - cannot slice unkown with ?.

Open mdmcconnell opened this issue 4 months ago • 2 comments

I wouldn't say that this is a bug, but an unexpected behaviour I have come across:

let x = "test"; x?.[0:1]

Returns "t", as expected.

let x = nil; x?.[0:1]

Returns "error cannot slice unknown (1:17)". I expected nil.

Possibly the first expression is misuse of the langauge and should not be allowed. But if it is to be allowed, then I think the consistent result of the second expression should be nil.

mdmcconnell avatar Aug 27 '25 18:08 mdmcconnell

It must be a desgin choice that let x = "test"; x[5:10] returns an empty string, versus go panicking, but then should let x = "test"; x?.[5:10] Also return an empty string, instead of nil?

mdmcconnell avatar Aug 27 '25 18:08 mdmcconnell

This is a very good point.

let x = "test"; x?.[5:10]

In this case I think returning an empty string is correct behavior. This is the same as the first example. X is still present, but the slice range is empty string. Option of Chaining is not changing the behavior here.

let x = nil; x?.[0:1]

But in case of slicing a nail with optional chain we should return nil. I guess this is a missing part from a type checker. It should also check if it's optional chain operation and allow it to be passed to compiler. I will implement a fix for this.

antonmedv avatar Aug 28 '25 09:08 antonmedv