Hi!
I've got this weird behavior with overloading, could anyone please advise if it's a bug?
INim 0.4.1
Nim Compiler Version 1.0.6 [Windows: amd64] at C:\Users\moigagoo\.nimble\bin\nim.exe
nim> proc foo(x: object): int = x.i*2
nim> proc foo(x: var object) = x.i*=2
nim> type Foo = object
.... i: int
....
nim> let x = Foo(i: 3)
nim> var y = Foo(i: 4)
nim> echo foo(x)
6
nim> foo(y)
Error: ambiguous call; both inim_1580414387.foo(x: object) [declared in C:\Users\moigagoo\AppData\Local\Temp\inim_1580414387.nim(3, 6)] and inim_1580414387.foo(x: var object) [declared in C:\Users\moigagoo\AppData\Local\Temp\inim_1580414387.nim(4, 6)] match for: (Foo)
Why can't the compiler properly find the proc to call? It works with other types but not object.
A variable declared with var can be used as both a var and regular parameter.
The following is valid:
type Foo = object
a: int
proc bar(x: Foo) =
echo x.a
proc baz(x: var Foo) =
echo x.a
var y = Foo(a: 1)
bar(y)
baz(y)
https://play.nim-lang.org/#ix=28KD
Notice that both bar and baz can be called with y. If the two functions share the same name, then the call is ambiguous, because y is valid for both functions.
Var parameters are prioritized over non var parameters when passed mutable values. This may be a bug due to using the the generic object typeclass. This code works.
type Foo = object
i: int
proc foo(x: Foo): int = x.i*2
proc foo(x: var Foo) = x.i*=2
let x = Foo(i: 3)
var y = Foo(i: 4)
echo foo(x)
foo(y)
Thanks for the suggestion! Unfortunately, even the hack with generics didn't work in my case. Probably because one of those procs is calling the other, which causes this: `` Error: expression 'insert(obj, force)' has no type (or is ambiguous)``
Had to add a differently named proc: https://github.com/moigagoo/norm/blob/feature/58_insert_for_immutable_objects/src/norm/sqlite.nim#L170
Will report the issue to Nim.