www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get struct template types

reply "ed" <growlercab gmail.com> writes:
Hi,

given a struct like so:

struct S(alias N, T) {...}

is there a way to get the template parameters of S? Something 
like:

S.typetuple[0] == N,
S.typetuple[1] == T

I've had a look at std.typecons and std.typetuple but I don't see 
what I'm missing something and cannot see a way to do the above.

Thanks,
ed
Feb 04 2014
next sibling parent reply "evilrat" <evilrat666 gmail.com> writes:
On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:
 Hi,

 given a struct like so:

 struct S(alias N, T) {...}

 is there a way to get the template parameters of S? Something 
 like:

 S.typetuple[0] == N,
 S.typetuple[1] == T

 I've had a look at std.typecons and std.typetuple but I don't 
 see what I'm missing something and cannot see a way to do the 
 above.

 Thanks,
 ed
typeof(N).stringof will return type of N as string for details read http://dlang.org/expression.html
Feb 04 2014
parent reply "evilrat" <evilrat666 gmail.com> writes:
On Tuesday, 4 February 2014 at 09:39:48 UTC, evilrat wrote:
 On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:
 Hi,

 given a struct like so:

 struct S(alias N, T) {...}

 is there a way to get the template parameters of S? Something 
 like:

 S.typetuple[0] == N,
 S.typetuple[1] == T

 I've had a look at std.typecons and std.typetuple but I don't 
 see what I'm missing something and cannot see a way to do the 
 above.

 Thanks,
 ed
typeof(N).stringof will return type of N as string for details read http://dlang.org/expression.html
oops sorry, it's for another people. what are you trying to achieve? when passing types as template args it is still types, so again typeof should give you type for variables(in such cases there is typeof), otherwise type is type. also, afaik it is not possible to store types in tuple, but i think you can store variables in tuple and retrieve their types at runtime
Feb 04 2014
parent reply "ed" <growlercab gmail.com> writes:
On Tuesday, 4 February 2014 at 09:46:01 UTC, evilrat wrote:
 On Tuesday, 4 February 2014 at 09:39:48 UTC, evilrat wrote:
 On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:
 Hi,

 given a struct like so:

 struct S(alias N, T) {...}

 is there a way to get the template parameters of S? Something 
 like:

 S.typetuple[0] == N,
 S.typetuple[1] == T

 I've had a look at std.typecons and std.typetuple but I don't 
 see what I'm missing something and cannot see a way to do the 
 above.

 Thanks,
 ed
typeof(N).stringof will return type of N as string for details read http://dlang.org/expression.html
oops sorry, it's for another people. what are you trying to achieve? when passing types as template args it is still types, so again typeof should give you type for variables(in such cases there is typeof), otherwise type is type. also, afaik it is not possible to store types in tuple, but i think you can store variables in tuple and retrieve their types at runtime
I am trying to set up an isS, isSOfN or isSOfT so I can create functions that allow restricted types of S based on all, or only one of the template parameters. Like so (pseudo-D code) --- struct S(alias N, T) {} // // Hopefully it is clear what I am trying to do here enum isSOfN = template(alias N,STYPE) { static assert(N == STYPET.typetuple[0]); } enum isSOfT = template(T,STYPE) { static assert(T == STYPET.typetuple[1]); } // void f(T)(T t) if(isSOfN!(3, T)) { // allow S with N=3 and any T (see below) } ... ... auto s3i = S!(3,int); auto s3f = S!(3,float); s3i.f(); // Succeeds s3f.f(); // Succeeds auto S4i = S!(4,int); s4i.f(); // compile time error -- I'm looking into T.stringof now, I might be able to get it working with some compile-time string magic of D :) If this is a bad way to go about it please let me know. I'm trying to learn idiomatic D as I go. Thanks, ed
Feb 04 2014
parent "evilrat" <evilrat666 gmail.com> writes:
On Tuesday, 4 February 2014 at 09:58:53 UTC, ed wrote:
 On Tuesday, 4 February 2014 at 09:46:01 UTC, evilrat wrote:
 On Tuesday, 4 February 2014 at 09:39:48 UTC, evilrat wrote:
 On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:
 Hi,

 given a struct like so:

 struct S(alias N, T) {...}

 is there a way to get the template parameters of S? 
 Something like:

 S.typetuple[0] == N,
 S.typetuple[1] == T

 I've had a look at std.typecons and std.typetuple but I 
 don't see what I'm missing something and cannot see a way to 
 do the above.

 Thanks,
 ed
typeof(N).stringof will return type of N as string for details read http://dlang.org/expression.html
oops sorry, it's for another people. what are you trying to achieve? when passing types as template args it is still types, so again typeof should give you type for variables(in such cases there is typeof), otherwise type is type. also, afaik it is not possible to store types in tuple, but i think you can store variables in tuple and retrieve their types at runtime
I am trying to set up an isS, isSOfN or isSOfT so I can create functions that allow restricted types of S based on all, or only one of the template parameters. Like so (pseudo-D code) --- struct S(alias N, T) {} // // Hopefully it is clear what I am trying to do here enum isSOfN = template(alias N,STYPE) { static assert(N == STYPET.typetuple[0]); } enum isSOfT = template(T,STYPE) { static assert(T == STYPET.typetuple[1]); } // void f(T)(T t) if(isSOfN!(3, T)) { // allow S with N=3 and any T (see below) } ... ... auto s3i = S!(3,int); auto s3f = S!(3,float); s3i.f(); // Succeeds s3f.f(); // Succeeds auto S4i = S!(4,int); s4i.f(); // compile time error -- I'm looking into T.stringof now, I might be able to get it working with some compile-time string magic of D :) If this is a bad way to go about it please let me know. I'm trying to learn idiomatic D as I go. Thanks, ed
for this simple case static if is pretty straightforward, but then if only purpose is to construct type it is better use regular generic function and typecons i think. ------------------------------ import std.stdio; struct S(alias N, T) { T x; void f() { static if(N==3) { writeln("n = 3, type = ", T.stringof); } else static if (N == 2) { writeln("n = 2, type = ", T.stringof); } else static assert(0, "NO!"); } } void main() { S!(3, int) a; // ok S!(2, float) b; // ok S!(4, double) c; // oops a.f(); b.f(); } ------------------------------
Feb 04 2014
prev sibling parent reply "Jakob Ovrum" <jakobovrum gmail.com> writes:
On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:
 Hi,

 given a struct like so:

 struct S(alias N, T) {...}

 is there a way to get the template parameters of S? Something 
 like:

 S.typetuple[0] == N,
 S.typetuple[1] == T

 I've had a look at std.typecons and std.typetuple but I don't 
 see what I'm missing something and cannot see a way to do the 
 above.

 Thanks,
 ed
Use type deduction: --- struct S(size_t n, T) {} enum isS(T) = is(T == S!(n, U), size_t n, U); template isS(T, size_t n) { static if(is(T == S!(n2, U), size_t n2, U)) enum isS = n == n2; else enum isS = false; } alias MyS = S!(3, float); static assert(isS!MyS); static assert(isS!(MyS, 3)); static assert(is(MyS == S!(3, float))); ---
Feb 04 2014
parent "ed" <growlercab gmail.com> writes:
On Tuesday, 4 February 2014 at 11:01:01 UTC, Jakob Ovrum wrote:
 On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:
 Hi,

 given a struct like so:

 struct S(alias N, T) {...}

 is there a way to get the template parameters of S? Something 
 like:

 S.typetuple[0] == N,
 S.typetuple[1] == T

 I've had a look at std.typecons and std.typetuple but I don't 
 see what I'm missing something and cannot see a way to do the 
 above.

 Thanks,
 ed
Use type deduction: --- struct S(size_t n, T) {} enum isS(T) = is(T == S!(n, U), size_t n, U); template isS(T, size_t n) { static if(is(T == S!(n2, U), size_t n2, U)) enum isS = n == n2; else enum isS = false; } alias MyS = S!(3, float); static assert(isS!MyS); static assert(isS!(MyS, 3)); static assert(is(MyS == S!(3, float))); ---
This is it! Thanks, ed
Feb 04 2014