April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Murphy | No. Current implementation doesn't enforce it. Kenji Hara 2012$BG/(B4$B7n(B12$BF|(B18:21 Daniel Murphy <yebblies@nospamgmail.com>: > "kenji hara" <k.hara.pg@gmail.com> wrote in message news:mailman.1646.1334209001.4860.digitalmars-d@puremagic.com... >> It conflicts with $B!I(Balias this$B!I(B syntax. >> >> template Inherits(T) >> { >> T value; >> alias T Inherits; // (1) Inherits!int == int >> alias T this; // (2) declares "alias this"! >> } >> struct S { >> mixin Inherits!int; // (1) invalid or (2) mixing alias this declaration? >> } >> >> Kenji Hara >> > > Doesn't it need to be explicitly marked as a 'mixin template' for it to be mixed in? > > |
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | "Jacob Carlborg" <doob@me.com> wrote in message news:jm66st$2d3f$1@digitalmars.com... > On 2012-04-12 11:21, Daniel Murphy wrote: > >> Doesn't it need to be explicitly marked as a 'mixin template' for it to >> be >> mixed in? >> >> > > No, I don't think that is forced. > > -- > /Jacob Carlborg Hmm. I guess the 'template' keyword is always available. |
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reid Levenick | On 12/04/12 04:21, Reid Levenick wrote:
> Firstly, I had no idea where suggestions should go, and I saw a few
> others here and thus here I am.
>
> I was writing some code that depended heavily on my own eponymous
> templates, and decided to change the names of some of them to make them
> more self-documenting. However, after changing the name, I encountered a
> long stream of unintelligible errors (to me, I haven't been using D for
> a long time) about template instances.
>
> So, my idea is that the 'this' keyword could be used in templates as a
> shortcut for eponymous templates, allowing code like this
>
> template anEponymousTemplate( size_t whatever ) {
> enum this = whatever * 2;
> }
> template anotherOne( T ) {
> static if( is( T == class ) ) {
> alias long this;
> } else {
> alias int this;
> }
> }
>
> Which would reduce cruft and make it easier to read some templates, as
> well as reducing maintenance.
This has been suggested about 200 times...
As already mentioned, use of 'this' conflicts with 'alias this'.
I also think there are problems with local templates.
class S
{
void foo()
{
template XX(int n) {
alias this this; // !! first 'this' is S, second is XX
}
XX!(7) q;
}
}
Another suggestion that was made in the past was 'return'. I suspect that doesn't work, either, because for example typeof(return) already has a meaning.
|
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Murphy | On Thu, Apr 12, 2012 at 07:31:11PM +1000, Daniel Murphy wrote: > "Jacob Carlborg" <doob@me.com> wrote in message news:jm66st$2d3f$1@digitalmars.com... > > On 2012-04-12 11:21, Daniel Murphy wrote: > > > >> Doesn't it need to be explicitly marked as a 'mixin template' for it to be mixed in? > >> > >> > > > > No, I don't think that is forced. > > > > -- > > /Jacob Carlborg > > Hmm. I guess the 'template' keyword is always available. [...] What about nested templates? Or perhaps we can combine the keywords: template MyTemplate(T) { static if (SomeCond!T) alias T this template; else static assert(0); } Clear, self-documenting, and unambiguous. :-) T -- "You know, maybe we don't *need* enemies." "Yeah, best friends are about all I can take." -- Calvin & Hobbes |
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | +1 for This "bearophile" wrote in message news:bcqvpvgjswwdgsfxsars@forum.dlang.org... Reid Levenick: > So, my idea is that the 'this' keyword could be used in templates as a shortcut for eponymous templates, allowing code like this > > template anEponymousTemplate( size_t whatever ) { > enum this = whatever * 2; > } Other "This" (with upper case)? Bye, bearophile |
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | +1 for This "bearophile" wrote in message news:bcqvpvgjswwdgsfxsars@forum.dlang.org... Reid Levenick: > So, my idea is that the 'this' keyword could be used in templates as a shortcut for eponymous templates, allowing code like this > > template anEponymousTemplate( size_t whatever ) { > enum this = whatever * 2; > } Other "This" (with upper case)? Bye, bearophile |
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reid Levenick | On 04/12/2012 04:21 AM, Reid Levenick wrote:
> Firstly, I had no idea where suggestions should go, and I saw a few
> others here and thus here I am.
>
> I was writing some code that depended heavily on my own eponymous
> templates, and decided to change the names of some of them to make them
> more self-documenting. However, after changing the name, I encountered a
> long stream of unintelligible errors (to me, I haven't been using D for
> a long time) about template instances.
>
> So, my idea is that the 'this' keyword could be used in templates as a
> shortcut for eponymous templates, allowing code like this
>
> template anEponymousTemplate( size_t whatever ) {
> enum this = whatever * 2;
> }
> template anotherOne( T ) {
> static if( is( T == class ) ) {
> alias long this;
> } else {
> alias int this;
> }
> }
>
> Which would reduce cruft and make it easier to read some templates, as
> well as reducing maintenance.
The general idea is useful, but there are issues.
'this' is the wrong keyword for the job. A class or struct instance is very different from a template instance and the proposed usage would clash with existing and useful ones. (assuming that it would work for symbol lookups in addition to declarations)
'template' or maybe 'scope' would be superior choices.
Furthermore, what happens if such a 'this'-template is mixed in into another template?
|
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Thu, 12 Apr 2012 14:04:44 -0400, Timon Gehr <timon.gehr@gmx.ch> wrote:
> The general idea is useful, but there are issues.
>
> 'this' is the wrong keyword for the job. A class or struct instance is very different from a template instance and the proposed usage would clash with existing and useful ones. (assuming that it would work for symbol lookups in addition to declarations)
> 'template' or maybe 'scope' would be superior choices.
I think static is underused keyword, maybe it should have a dual meaning here.
In all seriousness, I'm not sure template is right either. Scope sounds like a very bad choice to me.
Are there any eponymous examples where the name of the template is the first token? If so that would clash with declaring a new template.
I would find this weird:
template X(T)
{
alias T template;
template t {
...
}
}
It looks like you are using template as an alias to T, but the second 'template' inside is using it for creating a new template.
Also, consider this:
template X(T)
{
class template {
static typeof(this) create() { return new typeof(this);}
}
}
I don't think you could realistically use any keyword instead of typeof(this), I really think the best solution is what we have, or use a new keyword.
Another option is to do something like:
@eponymous template X(T) // changed from Y(T)
{
class Y { } // oops forgot to change this to X!
}
Error: template X did not declare eponymous symbol
At least changes would be loud errors.
-Steve
|
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Thu, Apr 12, 2012 at 08:04:44PM +0200, Timon Gehr wrote: [...] > The general idea is useful, but there are issues. > > 'this' is the wrong keyword for the job. A class or struct instance is very different from a template instance and the proposed usage would clash with existing and useful ones. (assuming that it would work for symbol lookups in addition to declarations) 'template' or maybe 'scope' would be superior choices. What about "this template"? Clear, self-documenting, and unambiguous. template X(T) { alias const(T) this template; } T -- One disk to rule them all, One disk to find them. One disk to bring them all and in the darkness grind them. In the Land of Redmond where the shadows lie. -- The Silicon Valley Tarot |
April 12, 2012 Re: An idea to improve eponymous templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 04/12/2012 08:19 PM, Steven Schveighoffer wrote: > On Thu, 12 Apr 2012 14:04:44 -0400, Timon Gehr <timon.gehr@gmx.ch> wrote: > >> The general idea is useful, but there are issues. >> >> 'this' is the wrong keyword for the job. A class or struct instance is >> very different from a template instance and the proposed usage would >> clash with existing and useful ones. (assuming that it would work for >> symbol lookups in addition to declarations) >> 'template' or maybe 'scope' would be superior choices. > > I think static is underused keyword, maybe it should have a dual meaning > here. > > In all seriousness, I'm not sure template is right either. It is better than 'this', because it is unambiguous. I didn't say it was right. ;) > Scope sounds like a very bad choice to me. > Why? Templates are parameterized scopes. > Are there any eponymous examples where the name of the template is the > first token? No, there isn't even a declaration that starts with the identifier it defines. > If so that would clash with declaring a new template. > > I would find this weird: > > template X(T) > { > alias T template; > template t { > ... > } > } > symbol > It looks like you are using template as an alias to T, but the second > 'template' inside is using it for creating a new template. > > Also, consider this: > > template X(T) > { > class template { > static typeof(this) create() { return new typeof(this);} > } > } > > I don't think you could realistically use any keyword instead of > typeof(this), I really think the best solution is what we have, +1. > or use a new keyword. > We certainly don't have enough of those! > Another option is to do something like: > > @eponymous template X(T) // changed from Y(T) > { > class Y { } // oops forgot to change this to X! > } > > Error: template X did not declare eponymous symbol > > At least changes would be loud errors. > > -Steve Well, there is always the possibility of just doing it like this: template X(T) { enum X = result; // single line in template body that needs to change if there is a name change static if(is(T==int)) enum result = foo; else static if(is(T==real)) enum result = bar; else static if(is(T==double)) enum result = baz; else enum result = qux; } Unless someone can come up with a super-elegant way to resolve all the issues raised, I don't actually think that the proposal adds enough value. |
Copyright © 1999-2021 by the D Language Foundation