Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 16, 2016 Access template parameters? | ||||
---|---|---|---|---|
| ||||
Say you have class Foo(type1,type2) { .... } And a concrete Foo is passed as a parameter to another template, is there a way to get type1 and type2 from Foo so you can use them in the new template... For example.. class Bar(FooType) { FooType.type1 DoSomething() { ... } } or Even something like class Bar(FooType) : Baz!(FooType.type1) { FooType.type1 DoSomething() { ... } } |
February 16, 2016 Re: Access template parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gavin Maye | On Tuesday, 16 February 2016 at 18:34:40 UTC, Gavin Maye wrote: > Say you have > > class Foo(type1,type2) > { > .... > } > > And a concrete Foo is passed as a parameter to another template, is there a way to get type1 and type2 from Foo so you can use them in the new template... For example.. > > class Bar(FooType) > { > FooType.type1 DoSomething() { ... } > } > > or Even something like > > class Bar(FooType) : Baz!(FooType.type1) > { > FooType.type1 DoSomething() { ... } > } If I understand you correctly, you want pattern matching i.e, http://dpaste.dzfl.pl/d8de0a004f59 |
February 16, 2016 Re: Access template parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gavin Maye | On 02/16/2016 10:34 AM, Gavin Maye wrote: > Say you have > > class Foo(type1,type2) > { > .... > } > > And a concrete Foo is passed as a parameter to another template, is > there a way to get type1 and type2 from Foo so you can use them in the > new template... For example.. > > class Bar(FooType) > { > FooType.type1 DoSomething() { ... } > } > > or Even something like > > class Bar(FooType) : Baz!(FooType.type1) > { > FooType.type1 DoSomething() { ... } > } > > std.traits.TemplateArgsOf: http://dlang.org/phobos/std_traits.html#TemplateArgsOf import std.traits; class Foo(type1,type2) {} class Bar(FooType) { // pragma(msg, TemplateArgsOf!FooType); alias ReturnType = TemplateArgsOf!FooType[0]; ReturnType DoSomething() { return ReturnType.init; } } void main() { auto b = new Bar!(Foo!(string, double)); } However, there are other ways of achieving the same thing at least when returning from a function, e.g.: auto DoSomething() {} (Check out typeof(return) which may be useful in that case.) Ali |
February 16, 2016 Re: Access template parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 16 February 2016 at 19:00:19 UTC, Ali Çehreli wrote:
>
> std.traits.TemplateArgsOf:
>
> http://dlang.org/phobos/std_traits.html#TemplateArgsOf
>
> import std.traits;
>
> class Foo(type1,type2)
> {}
>
> class Bar(FooType)
> {
> // pragma(msg, TemplateArgsOf!FooType);
>
> alias ReturnType = TemplateArgsOf!FooType[0];
>
> ReturnType DoSomething() {
> return ReturnType.init;
> }
> }
>
> void main() {
> auto b = new Bar!(Foo!(string, double));
> }
>
> However, there are other ways of achieving the same thing at least when returning from a function, e.g.:
>
> auto DoSomething() {}
>
> (Check out typeof(return) which may be useful in that case.)
>
> Ali
Thanks Ali!
|
Copyright © 1999-2021 by the D Language Foundation