www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - An idea to improve eponymous templates

reply "Reid Levenick" <reid.whitewater+dlang gmail.com> writes:
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.
Apr 11 2012
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Apr 12, 2012 at 04:21:45AM +0200, Reid Levenick wrote:
 Firstly, I had no idea where suggestions should go, and I saw a few
 others here and thus here I am.

AFAIK this is as good a place as any other.
 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;
 }

+1. I like this idea. In fact, I've thought about proposing it before. :-) I don't like the idea of having to repeatedly retype the template name. I mean, the compiler already implicitly translates: int myfunc(T)(T args) { ... } to: template myfunc(T) { int myfunc(T args) { ... } } and nowadays pretty much *nobody* writes it the second way anymore. Having to repeatedly type the same identifiers is one of the things that puts me off so much about Java. Especially with its proliferation of overly long identifiers (and their respective *Wrappers thereof). T -- The richest man is not he who has the most, but he who needs the least.
Apr 11 2012
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Apr 11 2012
next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
+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
Apr 12 2012
prev sibling parent "Mehrdad" <wfunction hotmail.com> writes:
+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
Apr 12 2012
prev sibling next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 4/11/12 9:21 PM, Reid Levenick wrote:
 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;
 }
 }

Walter and I discussed this idea a while ago. It's a good one. To ease into things eponymous templates should work both ways for a while. We should define a little protocol for ideas that Walter approves but has higher-priority tasks to work on. Like DIPs that he gives the seal of approval to, so people know they can work on them. Andrei
Apr 11 2012
prev sibling next sibling parent "Nathan M. Swan" <nathanmswan gmail.com> writes:
On Thursday, 12 April 2012 at 02:21:46 UTC, 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.

I like the idea. To get rid of any confusion with keywords, name-mangling, or classes, I suggest having "this" being syntactic sugar for the name of the template. NMS
Apr 11 2012
prev sibling next sibling parent "Reid Levenick" <reid.whitewater+dlang gmail.com> writes:
On Thursday, 12 April 2012 at 04:28:01 UTC, Nathan M. Swan wrote:
 I like the idea. To get rid of any confusion with keywords, 
 name-mangling, or classes, I suggest having "this" being 
 syntactic sugar for the name of the template.

I expressed it badly at the time, but that is exactly what I meant, yeah.
Apr 11 2012
prev sibling next sibling parent Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
12.04.2012 6:21, Reid Levenick написал:
 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 idea already has it's own issue: http://d.puremagic.com/issues/show_bug.cgi?id=7364 born from: http://www.digitalmars.com/d/archives/digitalmars/D/Aliasing_of_template_results_155859.html -- Денис В. Шеломовский Denis V. Shelomovskij
Apr 11 2012
prev sibling next sibling parent reply kenji hara <k.hara.pg gmail.com> writes:
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

2012$BG/(B4$B7n(B12$BF|(B11:21 Reid Levenick
<reid.whitewater+dlang gmail.com>:
 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.

Apr 11 2012
parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"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?
Apr 12 2012
parent reply Jacob Carlborg <doob me.com> writes:
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
Apr 12 2012
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"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.
Apr 12 2012
prev sibling next sibling parent kenji hara <k.hara.pg gmail.com> writes:
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?

Apr 12 2012
prev sibling next sibling parent Don Clugston <dac nospam.com> writes:
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.
Apr 12 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Apr 12 2012
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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?
Apr 12 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Apr 12 2012
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Apr 12 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Apr 12 2012