digitalmars.D - alias this to a template function
- Adam D. Ruppe (16/16) Nov 11 2012 I'm just curious if this is supposed to eventually work:
- Peter Alexander (7/8) Nov 11 2012 How can the compiler possibly figure out what T is supposed to be?
- Adam D. Ruppe (16/18) Nov 11 2012 How does it figure out which alias this right now? Looking at the
- Timon Gehr (6/15) Nov 11 2012 Just how it figures out the parameter types of lambdas.
- Jonathan M Davis (4/25) Nov 11 2012 You can only alias types and variables. A templated type isn't a type un...
- Adam D. Ruppe (9/11) Nov 11 2012 This compiles today though:
- Jonathan M Davis (4/20) Nov 11 2012 Weird. I've never seen a situation before where an alias accepted a part...
- Manfred Nowak (4/5) Nov 11 2012 It seems to be like any non-instantiated function `template'---
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
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
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
On 11/11/2012 10:42 PM, Peter Alexander wrote:On Sunday, 11 November 2012 at 21:36:41 UTC, Adam D. Ruppe wrote: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.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.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
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
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
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: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 DavisYou 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
Jonathan M Davis wrote:where an alias accepted a partially-instantiated templateIt seems to be like any non-instantiated function `template'--- only declared within a non templatized `struct'. -manfred
Nov 11 2012