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

Bind no longer working in generic procs. #11800

Closed
solo989 opened this issue Jul 20, 2019 · 1 comment
Closed

Bind no longer working in generic procs. #11800

solo989 opened this issue Jul 20, 2019 · 1 comment

Comments

@solo989
Copy link
Contributor

solo989 commented Jul 20, 2019

Imagine you have two files foreignFile.nim and myFile.nim.

foreignFile.nim

template p[T:int|float](a : T) = 
  echo "Number:" & $a

template p(a : string) =
  echo "String:" & a

proc printVar*[T:int|float|string](a : T) = 
  p(a)

All the templates printVar should call are already defined but p isn't bound so it can potentially use new templates/procs/etc defined in the future.

myFile.nim

import foreignFile

var x = 0

template p(a : int) = 
  x += a
  echo x

p(7)

printVar(5)
printVar(5.0)
printVar("5.0")

In this example you've inadvertently overwritten p from myFile when aProc is passed an int.
It will echo 12 instead of Number:5.
You only wanted to call your new p definition directly.
In this case the generic printVar proc should probably be three seperate printVar definitions instead of

proc printVar*[T:int|float|string](a : T)

but you never know what your going to get when importing other peoples code.

@Araq
Copy link
Member

Araq commented Jul 21, 2019

Works as by the spec, p is overloaded and so is an "open sym choice". This is also not a regression afaict, or if it is, the compiler used to be wrong and now it's correct. Workaround:

proc printVar*[T:int|float|string](a : T) = 
  bind p
  p(a)

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

3 participants