memo
memo copied to clipboard
Why can't we {.memoized.} an exported top-level proc?
The following two files:
#file: importtest1
import memo
proc f*(a: int): int {.memoized.} =
42
and
# file: importtest2
import importtest1
echo f(5)
...give the error:
Error: 'export' is only allowed at top level
Is there a way to avoid this restriction? I would like to have some library functions memoized, which I use in other files.
The best way would be for the macro to handle the visibility modifier. But for now, what you can do is define a private memoized function, which should work fine, and then export an inline function that just calls the first one. Something like (not tried):
import memo
proc g(a: int): int {.memoized.} =
42
proc f*(a: int): int {.inline.} = g(a)
# or else
# template f*(a: int): int = g(a)
Thank you for the suggestion, and also for the excellent module which is a joy to use.