www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Argument deduction for alias templates

reply Paul <paultjeadriaanse gmail.com> writes:
While trying out eponymous alias templates, I noticed they don't 
feature argument deducitions at all, even though the compiler 
knows what types are given, as can be told from the exceptions 
it's giving. Additonally, eponymous alias templates seem to only 
function when at least 1 argument is given, even if default 
values are used, which is slightly disappointing as this is not 
the case for eponymous function templates.

Random example from the top of my head:
 alias Vec(Kind = float, int size = 1) = Kind[size];
 alias Vec2(int size = 1, Kind = float) = Kind[size];

 void foo(Kind = float, int size = 1)(Kind[size] a) {
 	writeln(a);
 }

 // Works
 char[5] a = "apple";
 foo(a);
 Vec!string b = ["0.3"];

 // Does not work
 Vec!float d = [0.1, 0.2];
 // mismatched array lengths, 1 and 2
 Vec2!1 c = ["apple"];
 // cannot implicitly convert expression ["apple"] of type 
 string[] to float[]
 Vec f = [0.4];
 // template app.main.Vec(Kind = float, int size = 1) is used as 
 a type
Jan 07
next sibling parent reply Paul <paultjeadriaanse gmail.com> writes:
To further expand on this, if I am to believe stackoverflow 
(which may sometimes be a mistake), it seems C++ is adding 
support for this (https://stackoverflow.com/a/41008415/12640952), 
if this is intentionally not part of D, has it been considered to 
be added, I didn't find it in the list of DIP's nor on the forum.
Jan 07
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 7 January 2021 at 16:41:23 UTC, Paul wrote:
 To further expand on this, if I am to believe stackoverflow 
 (which may sometimes be a mistake), it seems C++ is adding 
 support for this 
 (https://stackoverflow.com/a/41008415/12640952), if this is 
 intentionally not part of D, has it been considered to be 
 added, I didn't find it in the list of DIP's nor on the forum.
See: https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1023.md A recent discussion: https://forum.dlang.org/post/adaidrwkknzhehatukkt forum.dlang.org
Jan 07
parent Paul <paultjeadriaanse gmail.com> writes:
On Thursday, 7 January 2021 at 17:48:12 UTC, jmh530 wrote:
 On Thursday, 7 January 2021 at 16:41:23 UTC, Paul wrote:
 To further expand on this, if I am to believe stackoverflow 
 (which may sometimes be a mistake), it seems C++ is adding 
 support for this 
 (https://stackoverflow.com/a/41008415/12640952), if this is 
 intentionally not part of D, has it been considered to be 
 added, I didn't find it in the list of DIP's nor on the forum.
See: https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1023.md A recent discussion: https://forum.dlang.org/post/adaidrwkknzhehatukkt forum.dlang.org
Oh my that's a big discussion, thank you, I seem to have missed the postponed DIP's, sorry!
Jan 07
prev sibling parent ag0aep6g <anonymous example.com> writes:
On Thursday, 7 January 2021 at 16:18:53 UTC, Paul wrote:
 While trying out eponymous alias templates, I noticed they 
 don't feature argument deducitions at all, even though the 
 compiler knows what types are given, as can be told from the 
 exceptions it's giving. Additonally, eponymous alias templates 
 seem to only function when at least 1 argument is given, even 
 if default values are used, which is slightly disappointing as 
 this is not the case for eponymous function templates.
You're right that there is no deduction being done. Deduction only happens with function templates (and even then only in limited form). With other kinds of templates, you have to spell out the instantiation. You're not exactly right about having to supply at least one argument. You can instantiate a template without any arguments: `Foo!()`. But you have to type out the "!()" part.
Jan 07