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

Error on method call syntax within a template #7085

Closed
nitely opened this issue Jan 14, 2018 · 5 comments · Fixed by #22076
Closed

Error on method call syntax within a template #7085

nitely opened this issue Jan 14, 2018 · 5 comments · Fixed by #22076

Comments

@nitely
Copy link
Contributor

nitely commented Jan 14, 2018

Following code doesn't work:

# Module A
proc foo(a: string): string =
  return a

template baz*(a: string): string =
  var b = a.foo()
  b
# Module B
import A

echo baz("hello")
# xxx template/generic instantiation from here
# xxx Error: attempting to call undeclared routine: 'foo'

Works replacing the method call by a regular proc call. Also works as is when using the template within module A. Also works when exporting foo. I can only guess the compiler tries to resolve the method call too late. The method call limitations section of the manual does not mention this specific case.

@jangko
Copy link
Contributor

jangko commented Jan 17, 2018

this is a well known issue, many people already encounter it when using generics. no wonder it also affected templates.
usually, mixin can solve this kind of problem.

template baz*(a: string): string =
  mixin foo
  var b = a.foo()
  b

but it would be nice if the compiler can activate the mixin automatically inside template and generics without specifically typing it (like C++ template).

@nitely
Copy link
Contributor Author

nitely commented Jan 19, 2018

@jangko except now foo will be picked from caller scope (own, parent, or global) if it's defined there 🤕

# Module B
import A

proc foo(a: string): string =
  return "bye"

echo baz("hello")
# bye

@metagn
Copy link
Collaborator

metagn commented Apr 22, 2020

Old issue, but an answer was never given, it's bind foo not mixin foo, which also fixes many of these "private symbol in template" issues

@nitely
Copy link
Contributor Author

nitely commented Apr 23, 2020

@hlaaftana Using bind instead of mixin throws the same error.

@metagn
Copy link
Collaborator

metagn commented Apr 23, 2020

My bad, I was talking out of my ass, it doesn't fix these issues. No one mentioned bind before though so it might help understand what's happening to know that it doesn't work.

metagn added a commit to metagn/Nim that referenced this issue Jun 11, 2023
Araq pushed a commit that referenced this issue Jun 12, 2023
* fix dot calls with resolved symbols in templates

* make old code work

* fix custom number literals test

* remove leftover debug marker

* enable "bug 9" test too

* fix renderer, add test for #7085
narimiran pushed a commit that referenced this issue Jun 13, 2023
* fix dot calls with resolved symbols in templates

* make old code work

* fix custom number literals test

* remove leftover debug marker

* enable "bug 9" test too

* fix renderer, add test for #7085

(cherry picked from commit 71801c2)
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
* fix dot calls with resolved symbols in templates

* make old code work

* fix custom number literals test

* remove leftover debug marker

* enable "bug 9" test too

* fix renderer, add test for nim-lang#7085
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.

5 participants