memo icon indicating copy to clipboard operation
memo copied to clipboard

Why can't we {.memoized.} an exported top-level proc?

Open EyeCon opened this issue 4 years ago • 2 comments

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.

EyeCon avatar Oct 30 '21 16:10 EyeCon

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)

andreaferretti avatar Nov 02 '21 11:11 andreaferretti

Thank you for the suggestion, and also for the excellent module which is a joy to use.

EyeCon avatar Nov 02 '21 16:11 EyeCon