www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Aliasing of template results

reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
Hi,

Someone on IRC wanted to know the element type of an array type and the 
following code was suggsted:

template ElementType(T : T[])
{
     alias T ElementType;
}

He was confused about how ElementType!(int[]) could possibly equal int, 
until we explained that the alias does, in fact, represent the 'result' 
of the template.

So we started discussing whether a more intuitive syntax could be found. 
Someone suggsted:

template ElementType(T : T[])
{
     alias T template;
}

I personally believe this makes it perfectly clear that you're aliasing 
the template itself to T.

Thoughts?

-- 
- Alex
Jan 22 2012
next sibling parent sclytrack <sclytrack fake.com> writes:
On 01/22/2012 03:51 PM, Alex Rønne Petersen wrote:
 Hi,

 Someone on IRC wanted to know the element type of an array type and the
 following code was suggsted:

 template ElementType(T : T[])
 {
 alias T ElementType;
 }

 He was confused about how ElementType!(int[]) could possibly equal int,
 until we explained that the alias does, in fact, represent the 'result'
 of the template.

 So we started discussing whether a more intuitive syntax could be found.
 Someone suggsted:

 template ElementType(T : T[])
 {
 alias T template;
 }

 I personally believe this makes it perfectly clear that you're aliasing
 the template itself to T.

 Thoughts?
https://github.com/PhilippeSigaud/D-templates-tutorial BaseElementType maybe. I've just scan-read it.
Jan 22 2012
prev sibling next sibling parent reply Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
22.01.2012 18:51, Alex Rønne Petersen пишет:
 Hi,

 Someone on IRC wanted to know the element type of an array type and the
 following code was suggsted:

 template ElementType(T : T[])
 {
 alias T ElementType;
 }

 He was confused about how ElementType!(int[]) could possibly equal int,
 until we explained that the alias does, in fact, represent the 'result'
 of the template.

 So we started discussing whether a more intuitive syntax could be found.
 Someone suggsted:

 template ElementType(T : T[])
 {
 alias T template;
 }

 I personally believe this makes it perfectly clear that you're aliasing
 the template itself to T.

 Thoughts?
I like this `alias T template;` syntax. `alias T ElementType;` is like writing `myFunction = value;` in function `myFunction` instead of `return` statement.
Jan 22 2012
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 01/22/2012 07:07 PM, Denis Shelomovskij wrote:
 22.01.2012 18:51, Alex Rønne Petersen пишет:
 Hi,

 Someone on IRC wanted to know the element type of an array type and the
 following code was suggsted:

 template ElementType(T : T[])
 {
 alias T ElementType;
 }

 He was confused about how ElementType!(int[]) could possibly equal int,
 until we explained that the alias does, in fact, represent the 'result'
 of the template.

 So we started discussing whether a more intuitive syntax could be found.
 Someone suggsted:

 template ElementType(T : T[])
 {
 alias T template;
 }

 I personally believe this makes it perfectly clear that you're aliasing
 the template itself to T.

 Thoughts?
I like this `alias T template;` syntax. `alias T ElementType;` is like writing `myFunction = value;` in function `myFunction` instead of `return` statement.
That is actually how it works in Pascal.
Jan 22 2012
parent Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
22.01.2012 22:11, Timon Gehr пишет:
 On 01/22/2012 07:07 PM, Denis Shelomovskij wrote:
 22.01.2012 18:51, Alex Rønne Petersen пишет:
 Hi,

 Someone on IRC wanted to know the element type of an array type and the
 following code was suggsted:

 template ElementType(T : T[])
 {
 alias T ElementType;
 }

 He was confused about how ElementType!(int[]) could possibly equal int,
 until we explained that the alias does, in fact, represent the 'result'
 of the template.

 So we started discussing whether a more intuitive syntax could be found.
 Someone suggsted:

 template ElementType(T : T[])
 {
 alias T template;
 }

 I personally believe this makes it perfectly clear that you're aliasing
 the template itself to T.

 Thoughts?
I like this `alias T template;` syntax. `alias T ElementType;` is like writing `myFunction = value;` in function `myFunction` instead of `return` statement.
That is actually how it works in Pascal.
You get the point! As Russel Winder wrote in sqrt(2) discussion:
dsimcha wrote:
LOL and Pascal was my example of a bondage-and-discipline language.
...
Bondage. Discipline. Does this mean Lady Heather will take control?
Jan 22 2012
prev sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 22-01-2012 19:07, Denis Shelomovskij wrote:
 22.01.2012 18:51, Alex Rønne Petersen пишет:
 Hi,

 Someone on IRC wanted to know the element type of an array type and the
 following code was suggsted:

 template ElementType(T : T[])
 {
 alias T ElementType;
 }

 He was confused about how ElementType!(int[]) could possibly equal int,
 until we explained that the alias does, in fact, represent the 'result'
 of the template.

 So we started discussing whether a more intuitive syntax could be found.
 Someone suggsted:

 template ElementType(T : T[])
 {
 alias T template;
 }

 I personally believe this makes it perfectly clear that you're aliasing
 the template itself to T.

 Thoughts?
I like this `alias T template;` syntax. `alias T ElementType;` is like writing `myFunction = value;` in function `myFunction` instead of `return` statement.
This was exactly my thought too. -- - Alex
Jan 22 2012
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
A while ago there was a suggestion by Andrei to incorporate this sort of syntax:

template ElementType(T : T[])
{
   alias ElementType = T;
}

struct Foo(T)
{
    alias Type = T;
}

I think people agreed it was a nice syntax, but I don't know if anyone
tried to implement it.
Jan 22 2012
next sibling parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 22-01-2012 19:33, Andrej Mitrovic wrote:
 A while ago there was a suggestion by Andrei to incorporate this sort of
syntax:

 template ElementType(T : T[])
 {
     alias ElementType = T;
 }

 struct Foo(T)
 {
      alias Type = T;
 }

 I think people agreed it was a nice syntax, but I don't know if anyone
 tried to implement it.
It still feels wrong. Why am I overwriting an existing symbol? -- - Alex
Jan 22 2012
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
If you know about eponymous templates then it makes sense imo.
Jan 22 2012
prev sibling parent Manu <turkeyman gmail.com> writes:
On 22 January 2012 20:36, Alex R=C3=B8nne Petersen <xtzgzorex gmail.com> wr=
ote:

 On 22-01-2012 19:33, Andrej Mitrovic wrote:

 A while ago there was a suggestion by Andrei to incorporate this sort of
 syntax:

 template ElementType(T : T[])
 {

    alias ElementType =3D T;
 }

 struct Foo(T)
 {
     alias Type =3D T;
 }

 I think people agreed it was a nice syntax, but I don't know if anyone
 tried to implement it.
It still feels wrong. Why am I overwriting an existing symbol?
Was it me that raised this on IRC? I've also been discussing it on IRC the last few days, and it is very confusing. I can write code that works now, but I still for the life of me find coherent logic for the syntax in my head, and whether I'm aliasing something, or producing a constant using enum... one way I'm producing a type, the other way I'm producing a value. The same syntax can produce this disconnected result; feels very unnatural to me. Types and values feel like totally different things in my mind, perhaps this is my error? I think the problem for me is that I can't see clearly exactly what a template actually does, it feels like there's some magic involved, mainly in the result syntax, that makes it work somehow...
Jan 25 2012
prev sibling next sibling parent Nick Treleaven <nospam example.net> writes:
On 22/01/2012 18:33, Andrej Mitrovic wrote:
 A while ago there was a suggestion by Andrei to incorporate this sort of
syntax:

 template ElementType(T : T[])
 {
     alias ElementType = T;
 }

 struct Foo(T)
 {
      alias Type = T;
 }

 I think people agreed it was a nice syntax, but I don't know if anyone
 tried to implement it.
I think this is not really relevant to the OP question about changing eponymous template syntax though. By coincidence, I implemented 'alias bar = foo;' syntax yesterday in my local copy - see attached diff.
Jan 23 2012
prev sibling parent Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
22.01.2012 22:33, Andrej Mitrovic пишет:
 A while ago there was a suggestion by Andrei to incorporate this sort of
syntax:

 template ElementType(T : T[])
 {
     alias ElementType = T;
 }

 struct Foo(T)
 {
      alias Type = T;
 }

 I think people agreed it was a nice syntax, but I don't know if anyone
 tried to implement it.
Personally I don't like current Eponymous Template syntax. I have no claim to Alias syntax.
Jan 24 2012
prev sibling next sibling parent "Jesse Phillips" <jessekphillips+D gmail.com> writes:
On Sunday, 22 January 2012 at 14:51:36 UTC, Alex Rønne Petersen 
wrote:
 So we started discussing whether a more intuitive syntax could 
 be found. Someone suggsted:

 template ElementType(T : T[])
 {
    alias T template;
 }

 I personally believe this makes it perfectly clear that you're 
 aliasing the template itself to T.

 Thoughts?
Isn't alias this basically what is happening? template ElementType(T : T[]) { alias T this; } Aliasing this template to type T? Ok, since multiple alias this are technically allowed... but still...
Jan 23 2012
prev sibling parent Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
22.01.2012 18:51, Alex Rønne Petersen пишет:
 Hi,

 Someone on IRC wanted to know the element type of an array type and the
 following code was suggsted:

 template ElementType(T : T[])
 {
 alias T ElementType;
 }

 He was confused about how ElementType!(int[]) could possibly equal int,
 until we explained that the alias does, in fact, represent the 'result'
 of the template.

 So we started discussing whether a more intuitive syntax could be found.
 Someone suggsted:

 template ElementType(T : T[])
 {
 alias T template;
 }

 I personally believe this makes it perfectly clear that you're aliasing
 the template itself to T.

 Thoughts?
Added issue inspired by your post: http://d.puremagic.com/issues/show_bug.cgi?id=7364
Jan 24 2012