How about a special null template parameter?
Engine Machine via Digitalmars-d
digitalmars-d at puremagic.com
Sun Aug 21 14:21:22 PDT 2016
On Sunday, 21 August 2016 at 00:44:01 UTC, Timon Gehr wrote:
> On 20.08.2016 21:20, Engine Machine wrote:
>>>>
>>>> That is
>>>>
>>>> It would be nice to have something like
>>>>
>>>> alias Type = Type!();
>>>> class Type(T...): TypeParent!T if(T.length==1){
>>>> int x;
>>>> static if (T is Dog)
>>>> int y;
>>>> }
>>>
>>> I don't understand how this is related.
>>
>>
>> The only difference is the alias Type = Type!(); Again, D
>> can't do this
>> but the point is that it would be nice to have the alias. One
>> can't do
>> everything as a "library" solution.
>> ...
>
> I see what you are after (but this was not part of the original
> requirements :) ). I don't think there's a way to make a
> symbol act as both a type and a template.
Well, I see that a template with 0 parameters can act as a
"type", if you will.
Just like functions
void foo(T)(T x)
acts like a normal function foo(3) even though it is a templated
function.
In fact, I see very little difference between a template with 0
parameters and a type.
Type!() = Type
seems very natural an logical to me as long as Type isn't defined
anywhere else... but that is a problem in all cases(e.g., even
functions).
Maybe there is some good reason though that simplifies the
compiler.
>
>> Trying to expand your code results in some odd behavior:
>>
>>
>> public template TypeParent(P)
>> {
>> import std.traits;
>> alias T = TemplateArgsOf!P;
>> alias Seq(T...) = T;
>> static if (T.length == 0 || is(typeof(T[0]) ==
>> typeof(null)))
>> {
>> alias TypeParent = Seq!();
>> }
>> else
>> {
>> alias TypeParent = Seq!(P!(T[0..T.length-1]));
>> }
>> }
>>
>>
>> class Type(T...) : TypeParent!(Type!T)
>> {
>> int x;
>> static if (T.length >= 1 && T[0] is "Animal")
>> {
>> int y;
>> static if (T.length >= 2 && T[1] is "Dog")
>> {
>> int z;
>> static if (T.length >= 3&& T[2] is "Pug")
>> {
>> int s;
>> }
>> }
>>
>> }
>> }
>>
>>
>> void main()
>> {
>>
>> import std.traits;
>>
>> auto a = new Type!("Animal", "Dog", "Pug")();
>> Type!("Animal", "Dog") b = a;
>> Type!("Animal") c = b;
>>
>> a.s = 1;
>> b.z = 2;
>> c.y = 3;
>> }
>>
>> b and c are of type P!, not Type!
>
> It seems that this is a compiler bug. Is the problem just with
> getting a string representation of the type?
I think it is just that D isn't "unaliasing" the template
parameter, P in this case, to what it aliases... It makes it seem
like things are different. Why one would have to keep track of
internal template aliases is beyond me. D should rewrite the
internal parameters in to meaningful external scope parameters if
possible(which should be possible since it must know the input to
the template).
More information about the Digitalmars-d
mailing list