www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pass symbol to template?

reply "JS" <js.mdnq gmail.com> writes:
I have some templates to help initialize things, like a property 
getter and setter.

mixin(Property!("Name", int));

creates a property named Name with type int.

I'd like to be able to call it like

mixin(Property!(Name, int));

(Name is a symbol but undefined at the mixin site(since it will 
be defined once the mixin is done)

I doubt this is possible in any way because it requires the 
compiler to allow such undefined symbols but would be nice...

e.g.,

template Property(lazy alias name, type) { ... }

but who knows, maybe I'm wrong and there is some way to do this?
Jul 19 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 19 July 2013 at 08:08:25 UTC, JS wrote:
 I have some templates to help initialize things, like a 
 property getter and setter.

 mixin(Property!("Name", int));

 creates a property named Name with type int.

 I'd like to be able to call it like

 mixin(Property!(Name, int));

 (Name is a symbol but undefined at the mixin site(since it will 
 be defined once the mixin is done)

 I doubt this is possible in any way because it requires the 
 compiler to allow such undefined symbols but would be nice...

 e.g.,

 template Property(lazy alias name, type) { ... }

 but who knows, maybe I'm wrong and there is some way to do this?
There isn't a way AFAIK You can use the q{...} syntax though, which is particularly nice for passing large code strings.
Jul 19 2013
parent "JS" <js.mdnq gmail.com> writes:
On Friday, 19 July 2013 at 08:40:09 UTC, John Colvin wrote:
 On Friday, 19 July 2013 at 08:08:25 UTC, JS wrote:
 I have some templates to help initialize things, like a 
 property getter and setter.

 mixin(Property!("Name", int));

 creates a property named Name with type int.

 I'd like to be able to call it like

 mixin(Property!(Name, int));

 (Name is a symbol but undefined at the mixin site(since it 
 will be defined once the mixin is done)

 I doubt this is possible in any way because it requires the 
 compiler to allow such undefined symbols but would be nice...

 e.g.,

 template Property(lazy alias name, type) { ... }

 but who knows, maybe I'm wrong and there is some way to do 
 this?
There isn't a way AFAIK You can use the q{...} syntax though, which is particularly nice for passing large code strings.
I'm just passing short string literals to represent variable names... It's not a huge deal but I wanted to avoid the quotes if I could(since they serve no real purpose).
Jul 19 2013