digitalmars.D - template alias = parameter
- =?UTF-8?B?Ikx1w61z?= Marques" (32/32) Sep 17 2013 Could you please comment on the following behaviors? Are all of
- Dicebot (13/46) Sep 17 2013 `alias` is never inferred so error is expected. You did not
- =?UTF-8?B?Ikx1w61z?= Marques" (9/9) Sep 17 2013 Thanks. I think what is weird is that the template parameters are
- Dicebot (3/9) Sep 17 2013 It should not be possible as template instances with same
- =?UTF-8?B?Ikx1w61z?= Marques" (5/7) Sep 17 2013 Well, my long shot idea was to use an alias template parameter,
- Dicebot (12/19) Sep 17 2013 Any two different alias parameter values mean different template
- =?UTF-8?B?Ikx1w61z?= Marques" (7/18) Sep 17 2013 Yes, that's what I was saying (or trying? thanks anyway). The
Could you please comment on the following behaviors? Are all of them intended? Are they internally coherent, with respect to each other? 1) void foo(alias f)(int f) { writeln(f); } foo(42); Error: does not match template 2) ... foo!(42)(42); > 42 3) ... foo!(7)(42); > 42 4) void foo(alias f)(int g=f) { writeln(g); } foo(42); Error: does not match template 5) void foo(alias f=42)(int g=f) { writeln(g); } foo(); > 42
Sep 17 2013
On Tuesday, 17 September 2013 at 11:24:10 UTC, Luís Marques wrote:Could you please comment on the following behaviors? Are all of them intended? Are they internally coherent, with respect to each other?Looks fine to me.1) void foo(alias f)(int f) { writeln(f); } foo(42); Error: does not match template`alias` is never inferred so error is expected. You did not provide any template parameters to foo. `void foo(T)(T f)` would have worked because `T` can be inferred.2) ... foo!(42)(42); > 42Explicit template parameter + normal parameter. Name of normal parameter shadows alias name (would have expected a warning/error here tbh).3) ... foo!(7)(42); > 42Same as (2).4) void foo(alias f)(int g=f) { writeln(g); } foo(42); Error: does not match templateAgain, you do not provide template parameter, it can't match. `foo!(42)()` should have worked here.5) void foo(alias f=42)(int g=f) { writeln(g); } foo(); > 42Default parameters can be used everywhere so no need to provide any parameter explicitly.
Sep 17 2013
Thanks. I think what is weird is that the template parameters are allowed to have the same name as the normal parameters, which confused my mental model and made me wonder what was even the expected behavior. These examples came from a set of experiments where I was trying to do something which I expected would probably not be possible: make a template function generate different code 1) without varying the function types, 2) without explicitly instantiating the template using "!". (this was for compatibility purposes).
Sep 17 2013
On Tuesday, 17 September 2013 at 11:43:24 UTC, Luís Marques wrote:These examples came from a set of experiments where I was trying to do something which I expected would probably not be possible: make a template function generate different code 1) without varying the function types, 2) without explicitly instantiating the template using "!". (this was for compatibility purposes).It should not be possible as template instances with same template parameter list share same body in code gen.
Sep 17 2013
On Tuesday, 17 September 2013 at 11:46:07 UTC, Dicebot wrote:It should not be possible as template instances with same template parameter list share same body in code gen.Well, my long shot idea was to use an alias template parameter, that way different values would mean different template parameters. But it seems I can't make the alias template parameter implicitly match in any way.
Sep 17 2013
On Tuesday, 17 September 2013 at 11:50:56 UTC, Luís Marques wrote:On Tuesday, 17 September 2013 at 11:46:07 UTC, Dicebot wrote:Any two different alias parameter values mean different template instances. Static introspection demands it: ``` auto foo(alias T)() { static if (is(typeof(T) : int) && (T == 42)) return true; else return false; } ```It should not be possible as template instances with same template parameter list share same body in code gen.Well, my long shot idea was to use an alias template parameter, that way different values would mean different template parameters. But it seems I can't make the alias template parameter implicitly match in any way.
Sep 17 2013
On Tuesday, 17 September 2013 at 12:10:11 UTC, Dicebot wrote:Any two different alias parameter values mean different template instances. Static introspection demands it: ``` auto foo(alias T)() { static if (is(typeof(T) : int) && (T == 42)) return true; else return false; } ```Yes, that's what I was saying (or trying? thanks anyway). The problem is that I can't get that and implicit instantiation at the same time. I mean, if void foo(T)(T x) makes T=int when called with foo(42), I was hoping that maybe void foo(alias V)(int V) would make V=42. But no. Well, it was a long shot, but it was worth the try.
Sep 17 2013