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 Rnne 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.
...


Jan 22 2012
prev sibling next 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
next sibling parent =?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
prev sibling next sibling parent Nick Treleaven <nospam example.net> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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 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
prev sibling 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:
--00235429d6d80ed25304b75a472e
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

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... --00235429d6d80ed25304b75a472e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On 22 January 2012 20:36, Alex R=C3=B8nne Peters= en <span dir=3D"ltr">&lt;<a href=3D"mailto:xtzgzorex gmail.com">xtzgzorex g= mail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style= =3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div class=3D"im">On 22-01-2012 19:33, Andrej Mitrovic wrote:<br> </div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l= eft:1px #ccc solid;padding-left:1ex"><div class=3D"im"> A while ago there was a suggestion by Andrei to incorporate this sort of sy= ntax:<br> <br></div> template ElementType(T : T[])<br> {<div class=3D"im"><br> =C2=A0 =C2=A0alias ElementType =3D T;<br> }<br> <br> struct Foo(T)<br> {<br> =C2=A0 =C2=A0 alias Type =3D T;<br> }<br> <br> I think people agreed it was a nice syntax, but I don&#39;t know if anyone<= br> tried to implement it.<br> </div></blockquote> <br> It still feels wrong. Why am I overwriting an existing symbol?</blockquote>= <div><br></div><div>Was it me that raised this on IRC? I&#39;ve also been d= iscussing it on IRC the last few days, and it is very confusing.</div> <div>I can write code that works now, but I still for the life of me find= =C2=A0coherent=C2=A0logic for the syntax in my head, and whether I&#39;m al= iasing something, or producing a constant using enum... one way I&#39;m pro= ducing a type, the other way I&#39;m producing a value. The same syntax can= produce this disconnected result; feels very unnatural to me. Types and va= lues feel like totally different things in my mind, perhaps this is my erro= r?</div> <div><br></div><div>I think the problem for me is that I can&#39;t see clea= rly exactly what a template actually does, it feels like there&#39;s some m= agic involved, mainly in the result syntax, that makes it work somehow...</= div> </div> --00235429d6d80ed25304b75a472e--
Jan 25 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