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

converter fails to work with multiple generic types #4554

Open
jxy opened this issue Aug 1, 2016 · 3 comments
Open

converter fails to work with multiple generic types #4554

jxy opened this issue Aug 1, 2016 · 3 comments

Comments

@jxy
Copy link
Contributor

jxy commented Aug 1, 2016

With the following code

type
  G[N:static[int]] = object
    v: int
  F[N:static[int]] = object
    v: int
static:
  echo(G[1] is G[2])
  echo(G[1] is F[1])
converter G2int[N:static[int]](x:G[N]):int = x.v
converter F2int[N:static[int]](x:F[N]):int = x.v
proc p(x,y:int) = echo "p ",x," ",y
var
  g1 = G[1](v:1)
  g2 = G[2](v:20)
  f1 = F[1](v:1000)
  f2 = F[2](v:200)
p(g1,g2)    # Error: type mismatch: got (G[1], G[2])
p(f1,f2)    # Error: type mismatch: got (F[1], F[2])
p(g1,g1)    # compiles
p(f1,f1)    # compiles
p(g1,f1)    # compiles
p(f1,g1)    # compiles
p(g1,f2)    # compiles
p(f1,g2)    # compiles

the first two calls, p(g1,g2) and p(f1,f2), fail with type mismatch complained by the compiler. However, the other calls compile and run fine.

@jxy
Copy link
Contributor Author

jxy commented Aug 1, 2016

The compiler believes that G[1] is G[2], so after changing the converter definition, the following code actually works

type
  G[N:static[int]] = object
    v: int
  F[N:static[int]] = object
    v: int
static:
  echo(G[1] is G[2])
  echo(G[1] is F[1])
converter G2int(x:G[0]):int = x.v
converter F2int(x:F[0]):int = x.v
proc p(x,y:int) = echo "p ",x," ",y
var
  g1 = G[1](v:1)
  g2 = G[2](v:20)
  f1 = F[1](v:1000)
  f2 = F[2](v:200)
p(g1,g2)
p(f1,f2)
p(g1,g1)
p(f1,f1)
p(g1,f1)
p(f1,g1)
p(g1,f2)
p(f1,g2)

Somehow G[0] and F[0], or defined with any int, make the compiler correctly call the converter. Bug or feature?

@jxy
Copy link
Contributor Author

jxy commented Jun 24, 2021

Now with the devel branch, Nim no long thinks G[1] is G[2] nor G[1] is F[1]. Both of the examples above do not compile.

@n0bra1n3r
Copy link

I was about to file a bug but saw this issue. I think I have the same issue, but with normal generic parameters and not static ones:

type Obj[T] = object
  b: T

converter test1[T](a: Obj[T]): T = a.b

proc doStuff(a: int, b: float) = discard

doStuff(Obj[int](b: 1), Obj[float](b: 1.2)) # Error: type mismatch: got <Obj[system.int], Obj[system.float]>

As far as I'm aware this has been an issue forever, just hit it again today. I try very hard not to use converters much because of the bugs and magic, but when I do use them they provide some very nifty functionality.

There was a patch awhile ago that seemed related but didn't fix this specific issue: #9698

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

No branches or pull requests

4 participants