www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - alias this to a template function

reply "Adam D. Ruppe" <destructionator gmail.com> writes:
I'm just curious if this is supposed to eventually work:

struct Test {
	alias get this;
	T get(T)() {
		return T.init;
	}
}

void main() {
	Test t;
	int a = t;
}


Where the int a = t would be expanded to int a = t.get!int;

The TDPL book says multiple alias this is supposed to be allowed, 
so you can subtype multiple things, but I'm wondering if we're 
eventually going to get the template to do multiple subtypes with 
one function.
Nov 11 2012
next sibling parent reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Sunday, 11 November 2012 at 21:36:41 UTC, Adam D. Ruppe wrote:
 I'm just curious if this is supposed to eventually work:
How can the compiler possibly figure out what T is supposed to be? This will never work. It doesn't even work without the alias this. void main() { Test t; int a = t.get(); // cannot deduce T }
Nov 11 2012
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 11 November 2012 at 21:42:45 UTC, Peter Alexander 
wrote:
 How can the compiler possibly figure out what T is supposed to 
 be?
How does it figure out which alias this right now? Looking at the dmd source, it looks like it calls implicitCastTo, which eventually calls another function to resolve the alias this. The requested type is known to the implicit cast function, so it's possible to pass that on and use it as the template argument. Since I think knowing the requested type would be necessary to resolve multiple alias this, I figure it will be changed to pass it on eventually anyway. But actually instantiating the template is another step that doesn't match the normal dot behavior, as you pointed out, so that left me wondering if they were planning to do it or not. It'd be different than the normal deduction, but it'd be potentially useful too, expanding alias this to work on more than just a pre-written list of types.
Nov 11 2012
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 11/11/2012 10:42 PM, Peter Alexander wrote:
 On Sunday, 11 November 2012 at 21:36:41 UTC, Adam D. Ruppe wrote:
 I'm just curious if this is supposed to eventually work:
How can the compiler possibly figure out what T is supposed to be?
Just how it figures out the parameter types of lambdas. For example, defer the type checking, alias this lookup and template instantiation into the implicit conversion AST node.
 This will never work.
Maybe it wont.
 It doesn't even work without the alias this.

 void main() {
      Test t;
      int a = t.get(); // cannot deduce T
 }
Which only means it does not currently work.
Nov 11 2012
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, November 11, 2012 22:36:40 Adam D. Ruppe wrote:
 I'm just curious if this is supposed to eventually work:
 
 struct Test {
 	alias get this;
 	T get(T)() {
 		return T.init;
 	}
 }
 
 void main() {
 	Test t;
 	int a = t;
 }
 
 
 Where the int a = t would be expanded to int a = t.get!int;
 
 The TDPL book says multiple alias this is supposed to be allowed,
 so you can subtype multiple things, but I'm wondering if we're
 eventually going to get the template to do multiple subtypes with
 one function.
You can only alias types and variables. A templated type isn't a type until it's been fully instantiated. - Jonathan M Davis
Nov 11 2012
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 11 November 2012 at 22:25:11 UTC, Jonathan M Davis 
wrote:
 You can only alias types and variables. A templated type isn't 
 a type until it's been fully instantiated.
This compiles today though: struct Test{ alias get this; T get(T)() { return T.init; } } It is just useless because there's no way to instantiate it though the alias this (you must do .get!T).
Nov 11 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, November 11, 2012 23:28:21 Adam D. Ruppe wrote:
 On Sunday, 11 November 2012 at 22:25:11 UTC, Jonathan M Davis
 
 wrote:
 You can only alias types and variables. A templated type isn't
 a type until it's been fully instantiated.
This compiles today though: struct Test{ alias get this; T get(T)() { return T.init; } } It is just useless because there's no way to instantiate it though the alias this (you must do .get!T).
Weird. I've never seen a situation before where an alias accepted a partially- instantiated template. I'm _very_ surprised that that compiles. - Jonathan M Davis
Nov 11 2012
parent Manfred Nowak <svv1999 hotmail.com> writes:
Jonathan M Davis wrote:

 where an alias accepted a partially-instantiated template
It seems to be like any non-instantiated function `template'--- only declared within a non templatized `struct'. -manfred
Nov 11 2012