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

Template issue with try #19581

Closed
gryphis opened this issue Mar 3, 2022 · 3 comments
Closed

Template issue with try #19581

gryphis opened this issue Mar 3, 2022 · 3 comments

Comments

@gryphis
Copy link

gryphis commented Mar 3, 2022

Hi
I learned something from #19576 but run into a next issue while trying to provide a template for typed string conversion. Tree versions constructive on each other work fine if coded as procs but fail as templates, the shortest complete runable example attached:

template getTyped[T](val: string, parseType: proc (str: string): T): Option[T] =
  return (try: some(parseType(val)) except: none(typeof T))

works well as proc:

proc     getTyped(val: string, parseType: proc (str: string): int): Option[int] =
  return (try: some(parseType(val)) except: none(int))

is called as:

proc getint*(key: string): Option[int] =
  let found = getval(key)
  if found.isSome: return getTyped(found.get, parseInt)
  else: return none(int)

Using: nim 1.6.4

Thanks for help!
Andi
nimq1.zip

@ghost
Copy link

ghost commented Mar 3, 2022

Templates are code substitution, so if you have return in a template it'll get inserted as a return where the template was "called", so in the end you would get return return (try: some(parseType(val)) except: none(typeof T)).

This is not a Nim bug, and the fix would be to remove return from your template.

@gryphis
Copy link
Author

gryphis commented Mar 3, 2022 via email

@ghost
Copy link

ghost commented Mar 3, 2022

I assume that I can close this issue then :)

@ghost ghost closed this as completed Mar 3, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant