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 template behavior (bind vs. mixin) #3542

Open
gmpreussner opened this issue Nov 12, 2015 · 10 comments
Open

Inconsistent template behavior (bind vs. mixin) #3542

gmpreussner opened this issue Nov 12, 2015 · 10 comments

Comments

@gmpreussner
Copy link
Contributor

The following setup works:

a.nim

template foo*() = discard

b.nim

template bar*() = foo()

c.nim

import a, b

bar()

However, if we replace the template a.foo with an overload of an existing template, such as system.pop, the code no longer compiles, because the compiler only finds system.pop:

a.nim

template pop*() = discard

b.nim

template foobar*() = pop()

c.nim

import a, b

foobar()

Output:

Hint: system [Processing]
Hint: c [Processing]
Hint: a [Processing]
Hint: b [Processing]
c.nim(4, 7) template/generic instantiation from here
b.nim(5, 6) Error: type mismatch: got ()
but expected one of: 
system.pop(s: var seq[T])
@gmpreussner
Copy link
Contributor Author

Workaround:

b.nim

template foobar*() =
  mixin pop # force `pop` symbol to be open
  pop()

@Araq
Copy link
Member

Araq commented Nov 12, 2015

The compiler and the spec agree. In your first example 'foo' is unbound identifier because system doesn't have 'foo', so it is put into a 'mixin' context. In your second example 'pop' is not unbound, but it is not overloaded either, so it is put into a 'bind' context. The manual needs to be updated with an example like this, but apart from that it works as indented.

@gmpreussner
Copy link
Contributor Author

I feel that this is problematic though, because module b.nim cannot possibly know all existing symbols in advance. So it either needs to always use mixin for its template usages (which is cumbersome), or it may inadvertently break in the future if c.nim suddenly decided to import some other module that conflicts.

In other words: module b.nim can break by modifying unrelated module c.nim

@gmpreussner
Copy link
Contributor Author

a.nim

template blah*() = discard

b.nim

template foobar*() = blah()

c.nim

import a, b

foobar()

d.nim

proc blah*() = discard

This will compile, but if we add import d to c.nim, then b.nim will fail to compile.

@Araq
Copy link
Member

Araq commented Nov 12, 2015

Yup. It's really bad, but I know of no alternative solution that is as convenient in practice. And also in practice it hasn't come up.

@Araq
Copy link
Member

Araq commented Nov 12, 2015

This will compile, but if we add import d to c.nim, then b.nim will fail to compile.

This is a different topic though, IMO. The compiler is right and then it becomes ambiguous.

@gmpreussner
Copy link
Contributor Author

Well, it came up for me today while writing a library of reusable templates :)

Can't the compiler know that both template foo and proc foo exist, and then pick the best match?

@Araq
Copy link
Member

Araq commented Nov 12, 2015

Can't the compiler know that both template foo and proc foo exist, and then pick the best match?

But that is exactly what it does?

@gmpreussner
Copy link
Contributor Author

No, it doesn't seem to find the mixin template if a symbol exists:

but expected one of: 
system.pop(s: var seq[T])

Should have found a.pop as well, no?

@stale
Copy link

stale bot commented Aug 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Aug 4, 2020
@metagn metagn removed the stale Staled PR/issues; remove the label after fixing them label Sep 18, 2024
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