Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent generics behavior #3022

Closed
ghost opened this issue Jun 30, 2015 · 2 comments
Closed

Inconsistent generics behavior #3022

ghost opened this issue Jun 30, 2015 · 2 comments

Comments

@ghost
Copy link

ghost commented Jun 30, 2015

Consider this:

a.nim:

proc new*[T](_: typedesc[seq[T]], len = 0.Natural): seq[T] {.inline.} = new_seq[T](len)
discard seq[tuple[k: string, v: string]].new()

Yields no error, a.new is called instead of system.new. I think it is correct behavior because T: typedesc of system.new is more general and _: typedesc[seq[T]] of a.new[T] is more specific thus more specific variant gets selected.

a.nim:

import b
discard seq[tuple[k: string, v: string]].new()

b.nim:

proc new*[T](_: typedesc[seq[T]], len = 0.Natural): seq[T] {.inline.} = new_seq[T](len)

nim c b.nim yields Error: ambiguous call; both system.new(T: typedesc) and a.new(_: typedesc[seq[T]], len: Natural) match for: (typedesc[seq[tuple[k: string, v: string]]])

I think proc with more specific type should be preferred instea of generating ambigious call error. Practical usecase as seen from exampleis binding new() constructor to a type while that constructor has no parameters. So far this bug mandates writing seq[string].new(0) instead of seq[string].new() completely negating convenience of default parameter value.

@Araq
Copy link
Member

Araq commented Jun 30, 2015

The compiler also uses scope to disambiguate so if you are in the same module your alternative 'new' gets picked. This needs to be documented at least but I really don't like this behaviour. It's of course required for some template hacking to work. sigh

@ghost
Copy link
Author

ghost commented Jun 30, 2015

So what behavior would/should be in the end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant