-
-
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 sugar #11686
Alias sugar #11686
Conversation
IMO |
I would honestly prefer this: alias t3 = t.left.left.left Can we accomplish this using macros? Thank you for creating this though! Been wanting this for a long time. |
Just for reference, Emacs Lisp has |
@dom96 👍 what should work is to check for I'm not sure about the withAlias(lll, t.left.left.left):
withAlias(rrr, t.right.right.right):
echo lll, rrr vs block:
alias(lll = t.left.left.left)
alias(rrr = t.right.right.right)
echo lll, rrr |
I don't understand how this is any better to defining an alias with a template directly: type Node = ref object
left, right: Node
val: int
let t = Node(left: Node(left: Node(left: Node(val: 10))))
template t3: untyped = t.left.left.left
doAssert t3 == 10 I don't like to have yet another syntax to define an alias. |
@mratsim I tried doing |
@krux02 Aliases are used in places that favor conciseness in variable names, often single letter, so that the code logic is more apparent. Having a short aliasing macro helps with that. For example: for ix in 0 ..< 10:
alias(pos = game.renderer.ball[ix].pos)
if pos == 0:
discard compared to for ix in 0 ..< 10:
template pos: untyped = game.renderer.ball[ix].pos
if pos == 0:
discard When reading the code, the @bluenote10 seems good, I'll update the PR. |
If this sugar should go into the standard library, then it should be in canon with let and var sections, in other words I think this should be valid syntax: block:
alias:
lll = t.left.left.left
rrr = t.right.right.right
echo lll, rrr But again, I am not a big fan. I also am not a big fan of the |
-1 from me. People will complain about |
Won't people complain about it too if some package defines it instead of the stdlib? |
Maybe, but should we add anything to the stdlib then that somebody might otherwise have defined somewhere else? |
I'm sorry but I have to use my BDFL superpowers to reject this already. This is exactly the sort of stuff like |
/cc @mratsim I don't think it should be written this way:
proc main()=
var counter = 0
proc getFoo(): var int =
counter.inc
counter
alias(bar, getFoo())
echo bar # 1
echo bar # 2 # my version would print 1 here and still make `bar` an alias (same addr)
main() another bad case is case of expensive computations hidden inside; it wrongly gives the impression it's evaluated only once. I wrote another
It works not too bad but has the same drawback of not great syntax, which limits me wanting to use it much (although the syntax suggested about via What I really want is this syntax:
optional but would be good to have too:import bar
alias bar2 = bar
alias myTemplateAlias = someTemplate
alias myProcAlias = someProc
alias myMacroAlias = myMacro
# avoid the ugly: template myMacroAlias(a: varargs[untyped]): untyped = myMacro
# ditto with iterator, macro etc ;
var a = 1
alias a2 = a
a2.inc
assert a == 2 The only way IMO is to support this in the compiler. |
Enough people are confused by let vs var, then we would have alias vs let vs var... |
@Araq Imho
It's weird that currently |
This goes against Nim's philosophy. We do not fill the language with hundreds of features that can be easily implemented using macros (even if the macro implementation isn't perfect). |
You haven't seen the trouble lc caused internally.
Please write your syntactic sugar snippents and collect them in a nimble package. That is what Nim is about. |
better fix here:
|
The
alias
sugar is a popular demand:This creates an alias template defined and used the following way.
I can also introduce a scoped alias similar to
with
statements in Python.