-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
alias templates #21663
alias templates #21663
Conversation
My latest thoughts on this are: Why are nullary templates not automatically |
Should have been more clear (been having sleep problems), this is a different version from the old PR and the one exactly written in the RFC. I could write another RFC for this new version, though to me this seems like the natural conclusion. The alias syntax is used very broadly and commonly mixed with normal call syntax so there's no good reason to remove it for templates in general, so I agree if that was your sentiment. This version circumvents the normal call syntax entirely with the annotation instead of soft allowing the alias syntax or whatever; and also completely cannot be overloaded. This compiles with this PR and before: template foo: int = 3
echo foo == foo() # true But this doesn't compile with this PR: template foo: int {.alias.} = 3
echo foo() # error: cannot call 3 There's a couple benefits with opting out of the call syntax:
https://forum.nim-lang.org/t/5015
Personally this feature isn't essential to me because 1. I don't really need or use proc aliases 2. my only issue with "mimicking" has been straight up bugs forcing the call syntax which have since been fixed But it allows stuff people want for very little cost, is conceptually easier to grasp and remember than an unconstrained alias syntax imo (especially with the overloading limitations), simplifies code, gives good errors, maybe has performance benefit, and maybe more I agree that no matter what some kind of nullary flag set at the template declaration automatically is good, it would save some computation (currently reusing |
I don't want to see code like template minus: untyped {.alias.} = `-`
assert minus(bar) == -15
assert minus(bar, minus(bar)) == 30
It's obfuscation. You are supposed to use These pure aliases are more of an anti-feature than a feature. |
I can split this patch again into the If |
Split off the uncontroversial stuff into #21671. Marking as draft instead of closing in case we come up with something better (like the typed thing) |
Just closing for now, this is not a priority |
closes nim-lang/RFCs#466, succeeds #20064
This is unlikely to break any code or need an adaptation period, so it's fine to not rush it. (I hope I am not delaying a new release by piling up PRs)
{.alias.}
templates are templates that take no arguments and act likevar
/let
/const
/type
symbols in that:foo()
wherefoo
is an alias template now means whatfoo()()
would have meant beforeThis is a stricter definition than the one in #20064.
Normal templates and macros can still be called with the alias syntax under the same conditions as before, and these conditions are now computed at the template/macro declaration and saved into
sfNoalias
(reused symbol flag). IfsfNoalias
is included, then a template or macro cannot be called with the alias syntax. Currently this can also be triggered with the{.noalias.}
pragma but this is not particularly necessary, just compatible with the implementation detailThe alias syntax now also works for types (for both types of templates/macros).
Potential future directions:
deprecatedStmt
use this and remove the final use ofskAlias