Determine type of property

tsbockman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 1 21:24:36 PST 2016


On Tuesday, 2 February 2016 at 03:36:25 UTC, Steven Schveighoffer 
wrote:
> S has 3 different properties, x, y, z:
>
> struct S
> {
>    int x;
>    int y() { return 1;}
> }
>
> int z(S s) { return 1;}
>
> pragma(msg, typeof(S.init.x).stringof); // int
> pragma(msg, typeof(S.init.y).stringof); // int()
> pragma(msg, typeof(S.init.z).stringof); // int
>
> Is there a trait I can call/use to consistently get "int" from 
> all 3?
>
> -Steve

import std.traits, std.stdio;

alias PropertyType(T, string property) =
	ReturnType!(function(T x) { return mixin("x." ~ property); });

struct S
{
    int x;
    int y() { return 1;}
}

int z(S s) { return 1;}

void main() {
	writeln(PropertyType!(S, "x").stringof);
	writeln(PropertyType!(S, "y").stringof);
	writeln(PropertyType!(S, "z").stringof);
}

(DPaste: http://dpaste.dzfl.pl/0b03bc8ea11f)


More information about the Digitalmars-d-learn mailing list