digitalmars.D.learn - Get struct template types
- ed (11/11) Feb 04 2014 Hi,
- evilrat (3/15) Feb 04 2014 typeof(N).stringof will return type of N as string
- evilrat (8/29) Feb 04 2014 oops sorry, it's for another people.
- Jakob Ovrum (17/29) Feb 04 2014 Use type deduction:
- ed (4/39) Feb 04 2014 This is it!
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
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, edtypeof(N).stringof will return type of N as string for details read http://dlang.org/expression.html
Feb 04 2014
On Tuesday, 4 February 2014 at 09:39:48 UTC, evilrat wrote:On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote: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 runtimeHi, 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, edtypeof(N).stringof will return type of N as string for details read http://dlang.org/expression.html
Feb 04 2014
On Tuesday, 4 February 2014 at 09:46:01 UTC, evilrat wrote:On Tuesday, 4 February 2014 at 09:39:48 UTC, evilrat wrote: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, edOn Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote: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 runtimeHi, 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, edtypeof(N).stringof will return type of N as string for details read http://dlang.org/expression.html
Feb 04 2014
On Tuesday, 4 February 2014 at 09:58:53 UTC, ed wrote:On Tuesday, 4 February 2014 at 09:46:01 UTC, evilrat wrote: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(); } ------------------------------On Tuesday, 4 February 2014 at 09:39:48 UTC, evilrat wrote: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, edOn Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote: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 runtimeHi, 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, edtypeof(N).stringof will return type of N as string for details read http://dlang.org/expression.html
Feb 04 2014
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, edUse 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
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:This is it! Thanks, edHi, 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, edUse 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