Skip to content

usage of [] on seqs within a generic wrongly prioritizes user-defined converter + overload over system.[] #15276

Open
@Quelklef

Description

@Quelklef

If there is an overload for [] with the type [T](s: seq[T], n: int): T, then trying to .incl on a HashSet will fail.

Example

import sets

proc `[]`[T](s: seq[T], n: int): T =
  discard

var myset = initHashSet[int]()
myset.incl(1)

Current Output

/home/runner/bugxnim/main.nim(7, 6) template/generic instantiation of `incl` from here
/usr/local/lib/nim/pure/collections/setimpl.nim(52, 14) template/generic instantiation of `enlarge` from here
/usr/local/lib/nim/pure/collections/setimpl.nim(43, 16) template/generic instantiation of `rawInsert` from here
/usr/local/lib/nim/pure/collections/setimpl.nim(29, 10) Error: 'data[h].key' cannot be assigned to

Expected Output

No error

Possible Solution

Unclear to me

Additional Information

If you change the overload for [] to return a var T instead of a T, then there is no error. I assume, then, that the issue is that setimpl.nim(29, 10) is, for some reason, using my [] overload instead of the default.

It is unclear to me why this is happening, though I will admit it's been a few years since I've used Nim. Regardless, I find it highly unlikely that this is expected behaviour, especially since incl is a proc and not a template. Let me know if I am wrong.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions