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

Is it possible to "specialize" an existing procedure/template? #1

Open
Varriount opened this issue Aug 28, 2023 · 1 comment
Open

Comments

@Varriount
Copy link

Varriount commented Aug 28, 2023

Say I have an existing procedure like this:

proc foo(a, b, c, d: int) =
  echo a + b + c + d

Is it possible to use spread to create a version of this procedure (with the same name) that takes a body of arguments?

foo:
  a = 1
  b = 2
  c = 3
  d = 4
@metagn
Copy link
Owner

metagn commented Aug 28, 2023

As a bare minimum:

import spread

proc fooImpl(a, b, c, d: int) =
  echo a + b + c + d

template foo(args: untyped) =
  spread(fooImpl, args)
# or, to keep regular call syntax working:
from macros import unpackVarargs
template foo(args: varargs[untyped]) =
  spread.unpackVarargs(fooImpl, args)

foo:
  a = 1
  b = 2
  c = 3
  d = 4

The limitations are that foo needs an untyped parameter due to the normally invalid a = 1 etc statements, which means it can't be passed around as a proc value; and foo and fooImpl can't have the same name i.e. overload each other, because the fooImpl overload will have a typed parameter where the untyped argument is supposed to be which will again cause it to get typechecked.

As long as these limitations are acknowledged though, a way to generate these could be nice, maybe as simple as adding {.spread.} to a proc

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

2 participants