digitalmars.D.learn - What does the alias attribute do here
- Gary Willoughby (6/6) Feb 04 2014 What does the alias attribute do here:
- Adam D. Ruppe (37/39) Feb 04 2014 This specifically won't compile, alias params are only allowed in
- Gary Willoughby (2/42) Feb 04 2014 Ah great, thanks that makes perfect sense.
- Mike (3/8) Feb 04 2014 Thanks, Adam, for the thorough explanation. This was quite
What does the alias attribute do here: void foo(alias bar) { ... } What is the idea behind this attribute when used here?
Feb 04 2014
On Tuesday, 4 February 2014 at 17:09:02 UTC, Gary Willoughby wrote:What does the alias attribute do here: void foo(alias bar)This specifically won't compile, alias params are only allowed in a compile-time list. So void foo(alias bar)() { ... } would work. Anyway, what it does is you pass another symbol to the function/template and the alias parameter works as the same thing. So let's play with: void foo(alias bar)() { import std.stdio; writeln(bar); } void main() { int a = 20; foo!a(); // will print 20 } What happened is foo!a passed the /symbol/, not just the variable contents, the variable itself, as the alias parameter. An important difference between this and a regular int parameter is you can assign to it too: void foo(alias a)() { import std.stdio; writeln(a); a = 50; // this is as if we literally wrote cool = 50; in main() } void main() { int cool = 20; foo!cool(); assert(cool == 50); // passes } alias parameters differ from regular parameters because a regular parameter can only be a type name. An alias parameter can be another variable. You can also pass it functions and call them as if the user wrote the call themselves - no pointers/delegates involved.
Feb 04 2014
On Tuesday, 4 February 2014 at 17:17:13 UTC, Adam D. Ruppe wrote:On Tuesday, 4 February 2014 at 17:09:02 UTC, Gary Willoughby wrote:Ah great, thanks that makes perfect sense.What does the alias attribute do here: void foo(alias bar)This specifically won't compile, alias params are only allowed in a compile-time list. So void foo(alias bar)() { ... } would work. Anyway, what it does is you pass another symbol to the function/template and the alias parameter works as the same thing. So let's play with: void foo(alias bar)() { import std.stdio; writeln(bar); } void main() { int a = 20; foo!a(); // will print 20 } What happened is foo!a passed the /symbol/, not just the variable contents, the variable itself, as the alias parameter. An important difference between this and a regular int parameter is you can assign to it too: void foo(alias a)() { import std.stdio; writeln(a); a = 50; // this is as if we literally wrote cool = 50; in main() } void main() { int cool = 20; foo!cool(); assert(cool == 50); // passes } alias parameters differ from regular parameters because a regular parameter can only be a type name. An alias parameter can be another variable. You can also pass it functions and call them as if the user wrote the call themselves - no pointers/delegates involved.
Feb 04 2014
On Tuesday, 4 February 2014 at 17:17:13 UTC, Adam D. Ruppe wrote:This specifically won't compile, alias params are only allowed in a compile-time list. So void foo(alias bar)() { ... } would work. [...]Thanks, Adam, for the thorough explanation. This was quite helpful for me as well.
Feb 04 2014