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

Warn on repeated non-trivial evaluations in templates #23754

Open
arnetheduck opened this issue Jun 24, 2024 · 0 comments
Open

Warn on repeated non-trivial evaluations in templates #23754

arnetheduck opened this issue Jun 24, 2024 · 0 comments

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Jun 24, 2024

Summary

When passing parameters to templates, subsitution may cause the parameter to be evaluated multiple times - warn when this happens

Description

proc f(): int =
  echo "hello"
  42

template test(a: int): int =
  a + a

echo test(f())

In the above template, a is evaluated twice which may be expensive - in the example, "hello" is printed twice which in most cases is both undesirable and inefficient.

In most cases, it's problem with the template declaration: it should probably have captured a in a local variable.

However, this is only a problem when a evaluates to something non-trivial - ie test(425) is not a problem and should not cause an evaluation warning.

Similarly, sometimes the multi-eval is desired, thus there should be a way to disable the warning locally - test(a{.dirty.}: int) would be one option (or some other keyword) - another option would be a call-site annotation.

Alternatives

No response

Examples

No response

Backwards Compatibility

No response

Links

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants