Type value

Zhenya zheny at list.ru
Tue Dec 11 00:06:27 PST 2012


On Tuesday, 11 December 2012 at 07:50:10 UTC, js.mdnq wrote:
> On Tuesday, 11 December 2012 at 07:15:38 UTC, Zhenya wrote:
>> I'm sorry for my english.
>> I know that D != Python and my example don't need
>> any RTTI.
>>
>> D has alias this feature.My example shows that we can use 
>> alias this with types.
>> And I just want use this:
>>
>> struct Type(T)
>> {
>>   alias T m_type;
>>   alias m_type this;
>> }
>>
>> int main()
>> {
>>   Type!int Int;
>>   Int i;//should compile by definition of alias this
>> }
>
> My I ask, why not just use int directly?
>
> I'm not proficient enough with D yet but it seems to me that 
> the example does not make sense.
>
> alias T m_type;
> alias m_type this;
>
> should be equivalent to
>
> alias T this;
>
> which, in some sense makes struct Type(T) equal to T... which 
> is what you are thinking, I think.
>
> But! I also believe that structs are meant as value types and 
> are addressable.
>
> So when you do
>
> Int i; you are essentially doing something like
>
> int x;
> x i;
>
> which makes no sense.
>
> If you do this instead,
>
> 	alias Type!int Int;
> 	Int i;
>
> then it will compile, of course.
>
> But your way, when you do Int x; you are specifically trying to 
> create a instance of an object. Yet you are not wanting it to 
> be an object and I'm not sure what you are wanting x to be. 
> (another type? a type of a type?)

In my example Int is value of Type!int.
When I write
Int i;
If Int had not alias this it really would not have sense,because 
type expected
instead of 'Int'.But Int have alias this 'int',and can be 
replaced by it.
My reason is simple:
Operators are functions.But if I want declare some struct like 
std.TypeTuple
I will need opSlice that return type.
It is impossible.
But if my example compiled,I could return value,that can be used 
as type.
It's something like Andrei's Type2Type.





More information about the Digitalmars-d-learn mailing list