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

Objects subtype generics are bound using the most broad type and not the most specific type #18314

Closed
alaviss opened this issue Jun 21, 2021 · 3 comments · Fixed by #24525
Closed

Comments

@alaviss
Copy link
Collaborator

alaviss commented Jun 21, 2021

Example

type
  A = ref object of RootObj
  B = ref object of A
  C = ref object of B

proc foo[T: A](a: T) = echo "got A"
proc foo[T: B](b: T) = echo "got B"

var c = C()
foo(c)

Current Output

got A

Expected Output

got B

Additional Information

  • Found during development of CPS
$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-06-14
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: a266c549212d1d6e09dbfa01344edbca8b2f6222
active boot switches: -d:release -d:nimUseLinenoise
@alaviss alaviss added Generics Inheritance CPS bugs/pulls related to the cps project labels Jun 21, 2021
@alaviss
Copy link
Collaborator Author

alaviss commented Jun 21, 2021

Snippet demonstrating a similar issue:

type
  A = ref object of RootObj
  B = ref object of A
  C = ref object of B
  
proc foo[T: A](a: T) = echo "got A"
proc foo(b: B) = echo "got B"

var c = C()
foo(c)

However, in this case a concrete overload is overpowered by a generic overload.

@Araq
Copy link
Member

Araq commented Jun 21, 2021

Your second example works according to the spec, the first example should probably produce an ambiguity call.

@metagn
Copy link
Collaborator

metagn commented Nov 17, 2024

Works since 2.2

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

Successfully merging a pull request may close this issue.

3 participants