Return types of the methods of a struct

Quentin Ladeveze via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 19 09:09:36 PDT 2015


On Friday, 19 June 2015 at 14:42:59 UTC, Steven Schveighoffer 
wrote:
> On 6/19/15 10:13 AM, Quentin Ladeveze wrote:
>> On Friday, 19 June 2015 at 14:04:05 UTC, Daniel Kozák wrote:
>>>
>>> On Fri, 19 Jun 2015 13:52:52 +0000
>>> Quentin Ladeveze via Digitalmars-d-learn
>>> <digitalmars-d-learn at puremagic.com> wrote:
>>>
>>>> On Friday, 19 June 2015 at 13:38:45 UTC, Steven 
>>>> Schveighoffer wrote:
>>>> >
>>>> > Does this work for you, or is there a further expectation?
>>>> >
>>>> > auto asTuple() { return Tuple!(int, "a", ...)(a, b, > 
>>>> > stringValue);}
>>>> >
>>>> > -Steve
>>>>
>>>> In fact, I was trying to use traits to create the tuple
>>>> automatically and being able to add or remove methods to the
>>>> struct without breaking the asTuple method.
>>>> I used allMembers for the name of the methods, but I didn't 
>>>> found
>>>> anything for their return types or their values.
>>>>
>>>> Thank you.
>>> http://dlang.org/phobos/std_traits.html#ReturnType
>>> http://dlang.org/phobos/std_traits.html#ParameterTypeTuple
>>
>> These are interesting and can be useful, but allMembers 
>> returns strings
>> and not functions, so I can't apply ReturnType.
>
> It's a *compile time* string. D is able to do some amazing 
> things with this :)
>
> // assuming 'a' is the first member
> mixin("alias aReturnType = ReturnType!(Example." ~ 
> __traits(allMembers, Example)[0] ~ ");");
> static assert(is(aReturnType == int));
>
> Using foreach over allMembers, you can construct a perfect 
> return tuple using introspection and mixin.
>
> -Steve

I would never have thought about mixins, this is amazing ! But 
thinking at compile time still is a little difficult for me, and 
now I don't understand how you can construct your tuple. I've 
been trying to create a string, by iterating on allMembers and 
concatenating the result of my functions in a string. But of 
course, I cannot use this string at compile time.

Now I know there is a way t do it, but my brain just can't figure 
it out.

Can you help me ?


More information about the Digitalmars-d-learn mailing list