Template type reduction

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 16 12:28:02 PDT 2016


On Tuesday, 16 August 2016 at 19:23:51 UTC, Engine Machine wrote:
> On Tuesday, 16 August 2016 at 17:39:14 UTC, Lodovico Giaretta 
> wrote:
>> On Monday, 15 August 2016 at 19:31:14 UTC, Engine Machine 
>> wrote:
>>> [...]
>>
>> I don't know if this is exactly what you want:
>>
>> =====================================
>> import std.traits: hasMember;
>>
>> struct Q(T)
>>     if (hasMember!(T, "x"))
>> {
>>     T s;
>>
>>     @property auto x() { return s.x; }
>> }
>> auto makeQ(T)(auto ref T val)
>> {
>>     return Q!T(val);
>> }
>>
>> auto s = myTypeWithFieldX();
>> auto q = makeQ(s);
>> assert(q.x == s.x);
>> =====================================
>>
>> `Q` can store any type with an `x` field and gives access to 
>> it. The auxiliary function `makeQ` acts as a constructor for 
>> `Q` with template parameter deduction.
>
> I realize now that D simply cannot do what I ask directly 
> because It's type parameters are part of the type. It can't be 
> told that in some cases they are not. Any use of the 
> parameters, as I am using them, will result in this issue.
>
> I'm now trying a different method which builds the based types 
> using partial oop and partial CT code. The CT code is only for 
> performance and convenience anyways.

You are right in that it is not possible to strip part of the 
type informations.

> The only problem is Q is still defined by T. Your makeQ then 
> requires the type implicitly, which I don't necessarily have. 
> You've simply added complexity to the problem but the issue is 
> still there.

How is it possible that you don't have T? Your variable will 
surely have a type! Can you provide more background? Because 
maybe I'm not completely getting your point here...


More information about the Digitalmars-d-learn mailing list