Files
Nim/tests/specialops/tsetterfallbacksubscript.nim
metagn 8752392838 implement setter fallback for subscripts (#24872)
follows up #24871

For subscript assignments, if an overload of `[]=`/`{}=` is not found,
the LHS checks for overloads of `[]`/`{}` as a fallback, similar to what
field setters do since #24871. This is accomplished by just compiling
the LHS if the assignment overloads fail. This has the side effect that
the error messages are different now, instead of displaying the
overloads of `[]=`/`{}=` that did not match, it will display the ones
for `[]`/`{}` instead. This could be fixed by checking for `efLValue`
when giving the error messages for `[]`/`{}` but this is not done here.

The code for `[]` subscripts is a little different because of the
`mArrGet`/`mArrPut` overloads that always match. If the `mArrPut`
overload matches without a builtin subscript behavior for the LHS then
it calls `semAsgn` again with `mode = noOverloadedSubscript`. Before
this meant "fail to compile" but now it means "try to compile the LHS as
normal", in both cases the overloads of `[]=` are not considered again.
2025-05-23 16:19:13 +02:00

26 lines
459 B
Nim

type Foo = object
x, y: float
proc `[]`(foo: var Foo, i: int): var float =
if i == 0:
result = foo.x
else:
result = foo.y
var pt = Foo(x: 0.0, y: 0.0)
pt[0] += 1.0 # <-- fine
`[]`(pt, 0) = 1.0 # <-- fine
pt[0] = 1.0 # <-- does not compile
# curly:
proc `{}`(foo: var Foo, i: int): var float =
if i == 0:
result = foo.x
else:
result = foo.y
pt{0} += 1.0 # <-- fine
`{}`(pt, 0) = 1.0 # <-- fine
pt{0} = 1.0 # <-- does not compile