Skip to content

Unexpected ambiguous call with fields over object with default fields #25252

@laylie527

Description

@laylie527

Nim Version

Nim Compiler Version 2.3.1 [Windows: amd64]
Compiled at 2025-11-01
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: 5079074
active boot switches: -d:release

Description

type
  Thing = object
    a: int = 100 # this is fine
    b = 100 # this is not

proc overloaded[T: SomeSignedInt](x: T) = discard
proc overloaded[T: SomeUnsignedInt](x: T) = discard
proc overloaded[T: object](x: T) =
  for val in fields(x):
    var v: typeof(val)
    overloaded(v)

overloaded(Thing())

Current Output

/repro.nim(13, 11) template/generic instantiation of `overloaded` from here
/repro.nim(11, 15) Error: ambiguous call; both repro.overloaded(x: T: SomeSignedInt) [proc declared in /repro.nim(6, 6)] and repro.overloaded(x: T: SomeUnsignedInt) [proc declared in /repro.nim(7, 6)] match for: (int literal(100))

Expected Output


Known Workarounds

  • Change b = 100 to b: int = 100.
  • Change var v: typeof(val) to var v = default typeof(val)

Additional Information

  • Requires all three overloaded to be generic.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions