0

Is it possible to get a reference to proc by its symbol for multi-dispatch proc?

I need to auto-generate some code based on function signature, and can't get the reference to the function.

run (seems like it's not showing output in the online playground)

import macros

macro generate_some_code*(fn: typed): typed =
  echo fn.tree_repr

proc multiply*(a: float, b: float): float = generate_some_code(multiply)

proc multiply*(a: string, b: string): string = generate_some_code(multiply)

discard multiply(1.0, 1.0)
discard multiply("A", "x")

Output:

Sym "multiply"
ClosedSymChoice
  Sym "multiply"
  Sym "multiply"

It has reference to the self in the first case, but not in the other. How to get the reference to self in the second case?

Alex Craft
  • 13,598
  • 11
  • 69
  • 133

1 Answers1

1

If you want to work with signature of proc then restructure your code like:

import macros

macro generate_some_code*(fn: typed): typed =
  echo fn.tree_repr

generate_some_code:
  proc multiply*(a: float, b: float): float = discard

generate_some_code:
  proc multiply*(a: string, b: string): string = discard

# assuming that generate_some_code will modify body and return it, so it is defined
discard multiply(1.0, 1.0)
discard multiply("A", "x")
Jakub Dóka
  • 2,477
  • 1
  • 7
  • 12