-
-
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 declarations #7090
Comments
This is possible with a template already, no? |
template inputs = game.inputs[event.key.keysym.scancode.toInput] |
Ah, thanks!
|
It probably will |
Doesn't look like it... This works, as per forum thread:
This doesn't:
It gets this error:
However, it looks like this does work, which is pretty nice:
|
However, this one sadly doesn't (it seems close to your original template example):
It gives this error:
This one does however:
However I think that syntax is a bit unsafe, ideally shouldn't be used. I've edited that forum thread with this updated info. |
You need to declare the return value. The error is saying that var a = 200
template b: untyped = a
b = 300
echo a |
Ah, thanks, that helps a lot! There's a section in the Nim manual which talks about untyped: https://nim-lang.org/docs/manual.html#overloading-resolution-lazy-type-resolution-for-untyped But it's not immediately obvious from that, that you can use template to swap in the left hand side of an assignment. However, looks like I can't say eg, substitute a proc directly:
Or eg rename modules, eg like this:
I think I'm asking for something pretty close to a source code string substitution macro, eg something like this, where "aliasing" is the macro, and "alias" is a special identifier recognized by the macro, like this: (kind of, operating a lot like how C's "define" preprocessor directive works):
|
The proc example can be done too. Note that specifying a return type (which can also be untyped) is only necessary if it returns a value. proc foo(x, y, z: int): int =
x + y + z
proc foo2(x, y, z: int) =
echo foo(x, y, z)
template bar(v: varargs[untyped]): untyped = foo(v)
template bar2(v: varargs[untyped]) = foo2(v)
echo bar(1,2,3)
bar2(1,2,3) I don't think there is a solution for the module alias at the moment. |
Ah, yes, and that even works in addition to other import names. import os
import os as my_os
echo os.dirExists("/tmp")
echo my_os.dirExists("/tmp") |
Workarounds have been sorted out. Closing. |
the workarounds don't work in many cases; this issue is properly fixed here: #11822 |
@timotheecour I'm going to repeat my request again, please stop cross-referencing your PRs across every single related issue. You can see that GitHub shows that you've referenced this issue above. Writing a comment here is just noise in our notifications. |
sure, will do. my reasoning was that issues may be referenced in a PR, but the PR is not intending to fix or close the issue referenced (eg workaround or just part of PR description). But fine. |
More or less what you have in C, with the 'define' preprocessor directive, but in a type-safe manner.
This may be possible in eg a macro or template that could be added to stdlib.
Basically what D describes over here:
https://dlang.org/spec/declaration.html#alias
Meaning that eg, code like this (from Platformer tut):
Can be expressed as something like this, for instance:
It's possible currently to get a lot of these kinds of effects by making use of some combination of const or template, or inlined function, but it's a bit untidy or verbose.
Here's one basic example of it from the Nim forum:
https://forum.nim-lang.org/t/1515
It's also something that Ada lets you do with its "rename" syntax, eg:
http://www.adaic.org/resources/add_content/docs/95style/html/sec_5/5-7-2.html
https://en.wikibooks.org/wiki/Ada_Programming/Basic#"Hello,_world!"_with_renames
The text was updated successfully, but these errors were encountered: