| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
March 23, 2008 Template question about alias | ||||
|---|---|---|---|---|
| ||||
Another D template question.
I am assuming that one can not create a variable which refers to a template instance, but one can create an "alias" for a template instance, ie.
template MyTemplate(T) { T data; /* other decldefs ... */};
MyTemplate!(int) aname; // illegal
alias MyTemplate!(int) aname; // legal
Is this correct ? If it is so, why did D not just scrap the use of 'alias' in this case and allow the first form, 'MyTemplate!(int) aname;' to represent an alias automatically, since it would save endlessly having to type 'alias' for a syntax which could not be anything else ?
| ||||
March 23, 2008 Re: Template question about alias | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Edward Diener | "Edward Diener" <eddielee_no_spam_here@tropicsoft.com> wrote in message news:fs4a59$2e5v$1@digitalmars.com... > Another D template question. > > I am assuming that one can not create a variable which refers to a template instance, but one can create an "alias" for a template instance, ie. > > template MyTemplate(T) { T data; /* other decldefs ... */}; > MyTemplate!(int) aname; // illegal > alias MyTemplate!(int) aname; // legal > > Is this correct ? If it is so, why did D not just scrap the use of 'alias' in this case and allow the first form, 'MyTemplate!(int) aname;' to represent an alias automatically, since it would save endlessly having to type 'alias' for a syntax which could not be anything else ? template MyTemplate(T) { alias T MyTemplate; } MyTemplate!(int) aname; // legal, typeof(aname) is int alias MyTemplate!(int) foo; // legal, foo is int foo bar; // bar is int In this case, the template is being used as a type. It's not legal _in some cases_. It is in others. It requires semantic analysis to know whether it is or not, so it's not something that can be restricted by syntax. | |||
March 23, 2008 Re: Template question about alias | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Edward Diener | Edward Diener wrote: > Another D template question. > > I am assuming that one can not create a variable which refers to a template instance, but one can create an "alias" for a template instance, ie. > > template MyTemplate(T) { T data; /* other decldefs ... */}; > MyTemplate!(int) aname; // illegal This looks like the declaration of a class template instantiated for int. So I'm glad that doesn't create an alias. It would be very confusing. > alias MyTemplate!(int) aname; // legal > > Is this correct ? If it is so, why did D not just scrap the use of 'alias' in this case and allow the first form, 'MyTemplate!(int) aname;' Because 'alias' is how you create type aliases in D. I don't get why you'd view dispensening of the 'alias' there a benefit. > to represent an alias automatically, since it would save endlessly having to type 'alias' for a syntax which could not be anything else ? --bb | |||
March 23, 2008 Re: Template question about alias | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> Edward Diener wrote:
>> Another D template question.
>>
>> I am assuming that one can not create a variable which refers to a template instance, but one can create an "alias" for a template instance, ie.
>>
>> template MyTemplate(T) { T data; /* other decldefs ... */};
>> MyTemplate!(int) aname; // illegal
>
> This looks like the declaration of a class template instantiated for int. So I'm glad that doesn't create an alias. It would be very confusing.
>
>> alias MyTemplate!(int) aname; // legal
>>
>> Is this correct ? If it is so, why did D not just scrap the use of 'alias' in this case and allow the first form, 'MyTemplate!(int) aname;'
>
> Because 'alias' is how you create type aliases in D. I don't get why you'd view dispensening of the 'alias' there a benefit.
If the first syntax was normally illegal, then having to type alias would be redundant if the first syntax were to be automatically considered an alias and be made legal. However I am told by another replier that the first syntax can sometimes be considered legal.
| |||
March 23, 2008 Re: Template question about alias | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Edward Diener | Edward Diener wrote:
> Bill Baxter wrote:
>> Edward Diener wrote:
>>> Another D template question.
>>>
>>> I am assuming that one can not create a variable which refers to a template instance, but one can create an "alias" for a template instance, ie.
>>>
>>> template MyTemplate(T) { T data; /* other decldefs ... */};
>>> MyTemplate!(int) aname; // illegal
>>
>> This looks like the declaration of a class template instantiated for int. So I'm glad that doesn't create an alias. It would be very confusing.
>>
>>> alias MyTemplate!(int) aname; // legal
>>>
>>> Is this correct ? If it is so, why did D not just scrap the use of 'alias' in this case and allow the first form, 'MyTemplate!(int) aname;'
>>
>> Because 'alias' is how you create type aliases in D. I don't get why you'd view dispensening of the 'alias' there a benefit.
>
> If the first syntax was normally illegal, then having to type alias would be redundant if the first syntax were to be automatically considered an alias and be made legal. However I am told by another replier that the first syntax can sometimes be considered legal.
I'm not sure what you're talking about anymore. But it sounds like maybe you figured it out for yourself in a later message. If not please ask again!
--bb
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply