digitalmars.D - Fallback 'catch-all' template functions
- Manu via Digitalmars-d (13/13) Aug 31 2016 So, consider a set of overloads:
- Ethan Watson (4/7) Sep 01 2016 +1. I've already hit this a few times with Binderoo. I would have
- Stefan Koch (12/25) Sep 01 2016 To my knowledge there is currently no clean way of doing this.
- Stefan Koch (9/16) Sep 01 2016 Was meant to be
- Andrea Fontana (4/21) Sep 01 2016 why not:
- Manu via Digitalmars-d (3/34) Sep 01 2016 This was my fallback plan, but it seems a bit shit.
- Stefan Koch (5/6) Sep 01 2016 Hmm I get your point.
- Manu via Digitalmars-d (9/17) Sep 01 2016 I know, but it's core language feature of D! Basically every function
- Dominikus Dittes Scherkl (20/33) Sep 01 2016 Simply
- Dominikus Dittes Scherkl (7/10) Sep 01 2016 I mean, overloads with same function signature except for the
- Cauterite (8/18) Sep 01 2016 When you're specialising on type classes rather than concrete
- Ethan Watson (10/12) Sep 01 2016 Oh, it's perfectly fine if you're not writing a library that's
- Dominikus Dittes Scherkl (11/20) Sep 01 2016 Ok, that may be fine, until you reach the point with the fallback
- Ethan Watson (6/11) Sep 01 2016 I don't see how that can be considered anything other than
- Manu via Digitalmars-d (3/26) Sep 01 2016 I think you must have not read the line of text that you replied to...
- Enamex (11/24) Sep 01 2016 An easy workaround with a bit of boilerplate would be:
- Steven Schveighoffer (20/33) Sep 01 2016 I agree. Note that if(isSomethingElse!T) may also need to have
- Meta (9/30) Sep 01 2016 I just thought of this, but cannot test if it works. If it does,
- Timon Gehr (14/21) Sep 01 2016 It shouldn't work, but DMD currently seems to allow it. (If you fix the
- Meta (5/30) Sep 01 2016 The idea is that there'd only be one such "fallback" template, so
- Timon Gehr (23/56) Sep 01 2016 I posted the ICE to show that DMD does not necessarily have a clear
- Meta (12/42) Sep 01 2016 Well, I'd argue that's not quite right and the correct
- Timon Gehr (5/49) Sep 01 2016 Even if that was the intention of the compiler implementation, the
- Meta (13/27) Sep 01 2016 It means to look up the symbol f at module scope, so I guess it
- Walter Bright (2/13) Sep 02 2016 Please post seg faults to bugzilla.
- Timon Gehr (4/20) Sep 03 2016 It had already been reported:
- Walter Bright (2/5) Sep 03 2016 Thank you!
- Cauterite (4/11) Sep 01 2016 That's so dirty, I love it. I imagine if __traits(getOverloads
- Xinok (8/21) Sep 02 2016 In the past, I have suggested using the "default" keyword to
- Manu via Digitalmars-d (3/9) Sep 02 2016 It's an interesting idea... flesh out a DIP?
- Andrei Alexandrescu (2/13) Sep 02 2016 We're better off without that. -- Andrei
- Manu via Digitalmars-d (9/27) Sep 02 2016 Then we need a decent way to do this.
- Andrei Alexandrescu (4/27) Sep 03 2016 Use static if inside the function. The entire notion of "call this
- Manu via Digitalmars-d (6/41) Sep 03 2016 It's all about: generic function 'lerp()' exists... user supplies new
- Andrei Alexandrescu (2/3) Sep 03 2016 std.conv.to is not a frequently done thing. -- Andrei
- Marco Leise (23/52) Sep 03 2016 This notion is what drives template specializations in D:
- Walter Bright (13/21) Sep 03 2016 import core.stdc.stdio;
- Manu via Digitalmars-d (3/27) Sep 03 2016 This is interesting. Can you explain how that works?
- Walter Bright (3/4) Sep 03 2016 Specializations are preferred over non-specializations, and T:T is the i...
- Jacob Carlborg (5/7) Sep 03 2016 What if the compiler can prefer template constraint in the same way?
- Walter Bright (6/11) Sep 03 2016 It's extremely risky to invent new template lookup rules for existing co...
- Marco Leise (6/12) Sep 03 2016 Pretty cool, I'll try that next time I write templated
So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D? I've asked this before, and people say: void f(T)(T t) if(!isSomething!T && !isSomethingElse!T) {} Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.
Aug 31 2016
On Thursday, 1 September 2016 at 05:37:50 UTC, Manu wrote:I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match.+1. I've already hit this a few times with Binderoo. I would have assumed that constraints are just another form of specialisation, but it requires me to be explicit with the base functionality.
Sep 01 2016
On Thursday, 1 September 2016 at 05:37:50 UTC, Manu wrote:So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D? I've asked this before, and people say: void f(T)(T t) if(!isSomething!T && !isSomethingElse!T) {} Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.To my knowledge there is currently no clean way of doing this. The easiest workaround would be to introduce another name for the implementation. then it would look like void f(T)(T t) { static if (is(fImpl(t) == void)) { f(t); } else { // default impl here } }
Sep 01 2016
On Thursday, 1 September 2016 at 08:44:38 UTC, Stefan Koch wrote:void f(T)(T t) { static if (is(fImpl(t) == void)) { f(t); } else { // default impl here } }Was meant to be void f(T)(T t) { static if (is(fImpl(t) == void)) { fImpl(t); } else { // default impl here } }
Sep 01 2016
On Thursday, 1 September 2016 at 08:46:07 UTC, Stefan Koch wrote:On Thursday, 1 September 2016 at 08:44:38 UTC, Stefan Koch wrote:why not: static if (__traits(compiles, fImpl(t))) ? Andreavoid f(T)(T t) { static if (is(fImpl(t) == void)) { f(t); } else { // default impl here } }Was meant to be void f(T)(T t) { static if (is(fImpl(t) == void)) { fImpl(t); } else { // default impl here } }
Sep 01 2016
On 1 September 2016 at 18:44, Stefan Koch via Digitalmars-d <digitalmars-d puremagic.com> wrote:On Thursday, 1 September 2016 at 05:37:50 UTC, Manu wrote:This was my fallback plan, but it seems a bit shit.So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D? I've asked this before, and people say: void f(T)(T t) if(!isSomething!T && !isSomethingElse!T) {} Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.To my knowledge there is currently no clean way of doing this. The easiest workaround would be to introduce another name for the implementation. then it would look like void f(T)(T t) { static if (is(fImpl(t) == void)) { f(t); } else { // default impl here } }
Sep 01 2016
On Thursday, 1 September 2016 at 09:08:31 UTC, Manu wrote:This was my fallback plan, but it seems a bit shit.Hmm I get your point. But there is not really another way within the current langauge. Also overloading lot of templates with templates constraints will eat into your compile-time!
Sep 01 2016
On 1 September 2016 at 19:16, Stefan Koch via Digitalmars-d <digitalmars-d puremagic.com> wrote:On Thursday, 1 September 2016 at 09:08:31 UTC, Manu wrote:I know, but it's core language feature of D! Basically every function in the std library has some constraints. It's critical for anything range based... Perhaps if concepts existed, it might be a more direct and less computationally intensive mechanism to deal with it. I've been feeling like that'd really simplify the situation a lot for quite a few years now.This was my fallback plan, but it seems a bit shit.Hmm I get your point. But there is not really another way within the current langauge. Also overloading lot of templates with templates constraints will eat into your compile-time!
Sep 01 2016
On Thursday, 1 September 2016 at 05:37:50 UTC, Manu wrote:So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D? I've asked this before, and people say: void f(T)(T t) if(!isSomething!T && !isSomethingElse!T) {} Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.Simply void f(T)(T t) { static if(isSomething!T) { } else static if(isSomethingElse!T) { } else { } } I personally hate overloads, especially if the condition has a fallback, so I like to see no condition in the function signature, what makes for a much cleaner API. I have never seen what benefit could be gained from having overloads. I think they are a relict from languages without static if.
Sep 01 2016
On Thursday, 1 September 2016 at 10:43:50 UTC, Dominikus Dittes Scherkl wrote:I have never seen what benefit could be gained from having overloads. I think they are a relict from languages without static if.I mean, overloads with same function signature except for the condition. Of course if the overloads have different parameters or return type, they may make sense. But they still uglyfy the API, so I try to avoid them - instead I use default parameters and template parameters what pretty much always works.
Sep 01 2016
On Thursday, 1 September 2016 at 10:50:18 UTC, Dominikus Dittes Scherkl wrote:On Thursday, 1 September 2016 at 10:43:50 UTC, Dominikus Dittes Scherkl wrote:When you're specialising on type classes rather than concrete types, you have no choice: auto f(T)(T source) if (is(ElementType!T : int)) {...}; auto f(T)(T source) if (is(ElementType!T : Object)) {...}; There is nothing you can write in the template parameters list to enable the same kind of specialisation.I have never seen what benefit could be gained from having overloads. I think they are a relict from languages without static if.I mean, overloads with same function signature except for the condition. Of course if the overloads have different parameters or return type, they may make sense. But they still uglyfy the API, so I try to avoid them - instead I use default parameters and template parameters what pretty much always works.
Sep 01 2016
On Thursday, 1 September 2016 at 10:43:50 UTC, Dominikus Dittes Scherkl wrote:I have never seen what benefit could be gained from having overloads.Oh, it's perfectly fine if you're not writing a library that's designed to allow user extension by going the "all in one" method. If you encourage your users to modify your function itself, they can no longer drop in a new version and have to do a merge. Templates in general seem weak for libraries in D, which is a pain considering templates are one of the areas where the language otherwise excels.
Sep 01 2016
On Thursday, 1 September 2016 at 10:53:01 UTC, Ethan Watson wrote:On Thursday, 1 September 2016 at 10:43:50 UTC, Dominikus Dittes Scherkl wrote:Ok, that may be fine, until you reach the point with the fallback version: if after that point someone "drops in" a new version, he silently changes the behavior of the function, because he "steals" some type which used to use the fallback version. --> overloads make only sense, if a function is NOT for all types, so you can add an overload for some type that was not considered so far, but you should not change the behaviour for some type that was already covered. At all, most of the time I prefer that the users directly change the function, yes.I have never seen what benefit could be gained from having overloads.Oh, it's perfectly fine if you're not writing a library that's designed to allow user extension by going the "all in one" method. If you encourage your users to modify your function itself, they can no longer drop in a new version and have to do a merge.
Sep 01 2016
On Thursday, 1 September 2016 at 11:01:28 UTC, Dominikus Dittes Scherkl wrote:Ok, that may be fine, until you reach the point with the fallback version: if after that point someone "drops in" a new version, he silently changes the behavior of the function, because he "steals" some type which used to use the fallback version.I don't see how that can be considered anything other than "expected behaviour" and thus ensure your design takes this in to account. If you give your user the keys to the kingdom, you need to expect them to use it.
Sep 01 2016
On 1 September 2016 at 20:43, Dominikus Dittes Scherkl via Digitalmars-d <digitalmars-d puremagic.com> wrote:On Thursday, 1 September 2016 at 05:37:50 UTC, Manu wrote:I think you must have not read the line of text that you replied to...Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.Simply void f(T)(T t) { static if(isSomething!T) { } else static if(isSomethingElse!T) { } else { } } I personally hate overloads, especially if the condition has a fallback, so I like to see no condition in the function signature, what makes for a much cleaner API. I have never seen what benefit could be gained from having overloads. I think they are a relict from languages without static if.
Sep 01 2016
On Thursday, 1 September 2016 at 05:37:50 UTC, Manu wrote:So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D? I've asked this before, and people say: void f(T)(T t) if(!isSomething!T && !isSomethingElse!T) {} Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.An easy workaround with a bit of boilerplate would be: void f(T)(T t) { static if(isSomething!T) { // special impl } else { fallback_f(t); } } void fallback_f(T)(T t) { ... }
Sep 01 2016
On 9/1/16 1:37 AM, Manu via Digitalmars-d wrote:So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D? I've asked this before, and people say: void f(T)(T t) if(!isSomething!T && !isSomethingElse!T) {} Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.I agree. Note that if(isSomethingElse!T) may also need to have if(!isSomething!T && isSomethingElse!T). A suggestion in the past was to allow "else" clauses with if constraints. I had envisioned: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) else if(isSomethingElse!T) {} void f(T)(T t) else {} But someone also suggested this more DRY solution as well (can't remember the thread for it): void f(T)(T t) if(isSomething!T) {} else if(isSomeghingElse!T) {} else {} Obviously this doesn't work across modules, but how does that even work? You need some sort of ordering for an if/else if/else scheme to work. Having a "fallback" template could potentially define a way to handle the default, but it doesn't fix the other issues. I don't know if it's because of the current rules, or just natural, but typically I'm not splitting my template functions between many modules. -Steve
Sep 01 2016
On Thursday, 1 September 2016 at 16:50:49 UTC, Steven Schveighoffer wrote:I agree. Note that if(isSomethingElse!T) may also need to have if(!isSomething!T && isSomethingElse!T). A suggestion in the past was to allow "else" clauses with if constraints. I had envisioned: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) else if(isSomethingElse!T) {} void f(T)(T t) else {} But someone also suggested this more DRY solution as well (can't remember the thread for it): void f(T)(T t) if(isSomething!T) {} else if(isSomeghingElse!T) {} else {} Obviously this doesn't work across modules, but how does that even work? You need some sort of ordering for an if/else if/else scheme to work. Having a "fallback" template could potentially define a way to handle the default, but it doesn't fix the other issues. I don't know if it's because of the current rules, or just natural, but typically I'm not splitting my template functions between many modules. -SteveI just thought of this, but cannot test if it works. If it does, maybe it would be a suitable solution? void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} //Taken if no other "overload" of f will intantiate with the given T void f(T)(T t) if(!__traits(compiles, alias _ = .f!T)) {}
Sep 01 2016
On 01.09.2016 19:21, Meta wrote:... I just thought of this, but cannot test if it works. If it does, maybe it would be a suitable solution? void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} //Taken if no other "overload" of f will intantiate with the given T void f(T)(T t) if(!__traits(compiles, alias _ = .f!T)) {}It shouldn't work, but DMD currently seems to allow it. (If you fix the syntax error.) I would expect it to break later. The following causes an ICE (DMD segfaults). import std.stdio; int f(T)(T t) if(!__traits(compiles,.f!T)) { return 0; } int f(T)(T t) if(!__traits(compiles,.f!T)) { return 1; } void main(){ writeln(f(2)); }
Sep 01 2016
On Thursday, 1 September 2016 at 17:49:13 UTC, Timon Gehr wrote:On 01.09.2016 19:21, Meta wrote:The idea is that there'd only be one such "fallback" template, so that you cannot get into a situation such as this. I'm guessing the ICE is due to a recursive dependency between the two f templates?... I just thought of this, but cannot test if it works. If it does, maybe it would be a suitable solution? void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} //Taken if no other "overload" of f will intantiate with the given T void f(T)(T t) if(!__traits(compiles, alias _ = .f!T)) {}It shouldn't work, but DMD currently seems to allow it. (If you fix the syntax error.) I would expect it to break later. The following causes an ICE (DMD segfaults). import std.stdio; int f(T)(T t) if(!__traits(compiles,.f!T)) { return 0; } int f(T)(T t) if(!__traits(compiles,.f!T)) { return 1; } void main(){ writeln(f(2)); }
Sep 01 2016
On 01.09.2016 19:55, Meta wrote:On Thursday, 1 September 2016 at 17:49:13 UTC, Timon Gehr wrote:I posted the ICE to show that DMD does not necessarily have a clear concept of how your code should be interpreted. Note that you are essentially saying: "If not X then X". It's very easy to run into behaviour that seems inconsistent once you do things like this. enum isSomething(T)=false; int f(T)(T t) if(isSomething!T){ return 0; } int f(T)(T t) if(!compiles!".f!int") { return 2; } enum compiles(string s) = __traits(compiles,mixin(s)); pragma(msg, compiles!".f!int"); // false pragma(msg, __traits(compiles,.f!int)); // true DMD cannot properly process code like this (i.e. code that contradicts itself, where the same expression in the same context can be true in one part of the compilation, but false later). Examples can be constructed where the semantics of the resulting code depends on the order that modules are passed on the command line. It's not specified anywhere what should happen, and it is not immediately clear. DMD shouldn't accept code like this in the first place. It's very brittle, the result depends on random compiler implementation details.On 01.09.2016 19:21, Meta wrote:The idea is that there'd only be one such "fallback" template, so that you cannot get into a situation such as this. I'm guessing the ICE is due to a recursive dependency between the two f templates?... I just thought of this, but cannot test if it works. If it does, maybe it would be a suitable solution? void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} //Taken if no other "overload" of f will intantiate with the given T void f(T)(T t) if(!__traits(compiles, alias _ = .f!T)) {}It shouldn't work, but DMD currently seems to allow it. (If you fix the syntax error.) I would expect it to break later. The following causes an ICE (DMD segfaults). import std.stdio; int f(T)(T t) if(!__traits(compiles,.f!T)) { return 0; } int f(T)(T t) if(!__traits(compiles,.f!T)) { return 1; } void main(){ writeln(f(2)); }
Sep 01 2016
On Thursday, 1 September 2016 at 18:24:13 UTC, Timon Gehr wrote:Well, I'd argue that's not quite right and the correct interpretation is "If not the other X then this X", due to the `!__traits(compiles, .f!T)`, explicitly telling the compiler to check if the *other* "overloads" compile. I don't actually know whether template constraints are considered to be at module scope or in the template scope, though, so maybe I'm completely wrong on this.The idea is that there'd only be one such "fallback" template, so that you cannot get into a situation such as this. I'm guessing the ICE is due to a recursive dependency between the two f templates?I posted the ICE to show that DMD does not necessarily have a clear concept of how your code should be interpreted. Note that you are essentially saying: "If not X then X". It's very easy to run into behaviour that seems inconsistent once you do things like this.enum isSomething(T)=false; int f(T)(T t) if(isSomething!T){ return 0; } int f(T)(T t) if(!compiles!".f!int") { return 2; } enum compiles(string s) = __traits(compiles,mixin(s)); pragma(msg, compiles!".f!int"); // false pragma(msg, __traits(compiles,.f!int)); // true DMD cannot properly process code like this (i.e. code that contradicts itself, where the same expression in the same context can be true in one part of the compilation, but false later). Examples can be constructed where the semantics of the resulting code depends on the order that modules are passed on the command line. It's not specified anywhere what should happen, and it is not immediately clear. DMD shouldn't accept code like this in the first place. It's very brittle, the result depends on random compiler implementation details.I would argue that this is an entirely different case than my example, but we are getting into compiler implementation details that I know nothing about, so I can't actually say whether it is (or should) be valid code.
Sep 01 2016
On 01.09.2016 21:02, Meta wrote:On Thursday, 1 September 2016 at 18:24:13 UTC, Timon Gehr wrote:Even if that was the intention of the compiler implementation, the example below demonstrates why it cannot work.Well, I'd argue that's not quite right and the correct interpretation is "If not the other X then this X", due to the `!__traits(compiles, .f!T)`, explicitly telling the compiler to check if the *other* "overloads" compile.The idea is that there'd only be one such "fallback" template, so that you cannot get into a situation such as this. I'm guessing the ICE is due to a recursive dependency between the two f templates?I posted the ICE to show that DMD does not necessarily have a clear concept of how your code should be interpreted. Note that you are essentially saying: "If not X then X". It's very easy to run into behaviour that seems inconsistent once you do things like this.I don't actually know whether template constraints are considered to be at module scope or in the template scope, though, so maybe I'm completely wrong on this. ...Template scope, but /nothing/ about '.' says "other".It's basically the same thing.enum isSomething(T)=false; int f(T)(T t) if(isSomething!T){ return 0; } int f(T)(T t) if(!compiles!".f!int") { return 2; } enum compiles(string s) = __traits(compiles,mixin(s)); pragma(msg, compiles!".f!int"); // false pragma(msg, __traits(compiles,.f!int)); // true DMD cannot properly process code like this (i.e. code that contradicts itself, where the same expression in the same context can be true in one part of the compilation, but false later). Examples can be constructed where the semantics of the resulting code depends on the order that modules are passed on the command line. It's not specified anywhere what should happen, and it is not immediately clear. DMD shouldn't accept code like this in the first place. It's very brittle, the result depends on random compiler implementation details.I would argue that this is an entirely different case than my example, but we are getting into compiler implementation details that I know nothing about, so I can't actually say whether it is (or should) be valid code.
Sep 01 2016
On Thursday, 1 September 2016 at 19:32:23 UTC, Timon Gehr wrote:It means to look up the symbol f at module scope, so I guess it depends on whether the compiler excludes the current template from that lookup. template f() if (someCondition) {} template f() if (someOtherCondition) {} template f() if (!__traits(compiles, { alias _ = .f!(); })) {} Does `.f` refer to all symbols named f at module scope, or all symbols named f *other than* the symbol for which the current template constraint is being processed? Actually, I just convinced myself that it's obviously not the latter and so must be the former, and now I see why this shouldn't work.Well, I'd argue that's not quite right and the correct interpretation is "If not the other X then this X", due to the `!__traits(compiles, .f!T)`, explicitly telling the compiler to check if the *other* "overloads" compile.Even if that was the intention of the compiler implementation, the example below demonstrates why it cannot work.I don't actually know whether template constraints are considered to be at module scope or in the template scope, though, so maybe I'm completely wrong on this. ...Template scope, but /nothing/ about '.' says "other".
Sep 01 2016
On 9/1/2016 10:49 AM, Timon Gehr wrote:The following causes an ICE (DMD segfaults). import std.stdio; int f(T)(T t) if(!__traits(compiles,.f!T)) { return 0; } int f(T)(T t) if(!__traits(compiles,.f!T)) { return 1; } void main(){ writeln(f(2)); }Please post seg faults to bugzilla.
Sep 02 2016
On 03.09.2016 05:15, Walter Bright wrote:On 9/1/2016 10:49 AM, Timon Gehr wrote:It had already been reported: https://issues.dlang.org/show_bug.cgi?id=11856 I have added this example to the report.The following causes an ICE (DMD segfaults). import std.stdio; int f(T)(T t) if(!__traits(compiles,.f!T)) { return 0; } int f(T)(T t) if(!__traits(compiles,.f!T)) { return 1; } void main(){ writeln(f(2)); }Please post seg faults to bugzilla.
Sep 03 2016
On 9/3/2016 2:19 AM, Timon Gehr wrote:It had already been reported: https://issues.dlang.org/show_bug.cgi?id=11856 I have added this example to the report.Thank you!
Sep 03 2016
On Thursday, 1 September 2016 at 17:21:02 UTC, Meta wrote:I just thought of this, but cannot test if it works. If it does, maybe it would be a suitable solution? void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} //Taken if no other "overload" of f will intantiate with the given T void f(T)(T t) if(!__traits(compiles, alias _ = .f!T)) {}That's so dirty, I love it. I imagine if __traits(getOverloads worked for templates we could actually use this pretty reliably (by excluding the fallback template from the search).
Sep 01 2016
On Thursday, 1 September 2016 at 05:37:50 UTC, Manu wrote:So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D? I've asked this before, and people say: void f(T)(T t) if(!isSomething!T && !isSomethingElse!T) {} Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.In the past, I have suggested using the "default" keyword to specify a fallback function of this kind. I think it's a useful pattern for generic algorithms that have optimized variants on specific types for performance. void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) default {}
Sep 02 2016
On 3 September 2016 at 00:18, Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:In the past, I have suggested using the "default" keyword to specify a fallback function of this kind. I think it's a useful pattern for generic algorithms that have optimized variants on specific types for performance. void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) default {}It's an interesting idea... flesh out a DIP?
Sep 02 2016
On 9/3/16 2:41 AM, Manu via Digitalmars-d wrote:On 3 September 2016 at 00:18, Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:We're better off without that. -- AndreiIn the past, I have suggested using the "default" keyword to specify a fallback function of this kind. I think it's a useful pattern for generic algorithms that have optimized variants on specific types for performance. void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) default {}It's an interesting idea... flesh out a DIP?
Sep 02 2016
On 3 September 2016 at 11:38, Andrei Alexandrescu via Digitalmars-d <digitalmars-d puremagic.com> wrote:On 9/3/16 2:41 AM, Manu via Digitalmars-d wrote:Then we need a decent way to do this. As I've just expressed in the ADL thread, this whole pattern, which is alleged as one of D's core offerings; templates + UFCS -> pipeline programming (or 'component' programming as Walter likes to call it), is loaded with issues, and other than SFINAE being a pita, and UFCS coming to C++ 'soon'™, the whole thing is much easier in C++ right now.On 3 September 2016 at 00:18, Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:We're better off without that. -- AndreiIn the past, I have suggested using the "default" keyword to specify a fallback function of this kind. I think it's a useful pattern for generic algorithms that have optimized variants on specific types for performance. void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) default {}It's an interesting idea... flesh out a DIP?
Sep 02 2016
On 9/3/16 7:29 AM, Manu via Digitalmars-d wrote:On 3 September 2016 at 11:38, Andrei Alexandrescu via Digitalmars-d <digitalmars-d puremagic.com> wrote:Use static if inside the function. The entire notion of "call this function if you can't find something somewhere that works" is questionable. -- AndreiOn 9/3/16 2:41 AM, Manu via Digitalmars-d wrote:Then we need a decent way to do this.On 3 September 2016 at 00:18, Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:We're better off without that. -- AndreiIn the past, I have suggested using the "default" keyword to specify a fallback function of this kind. I think it's a useful pattern for generic algorithms that have optimized variants on specific types for performance. void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) default {}It's an interesting idea... flesh out a DIP?
Sep 03 2016
On 3 September 2016 at 22:24, Andrei Alexandrescu via Digitalmars-d <digitalmars-d puremagic.com> wrote:On 9/3/16 7:29 AM, Manu via Digitalmars-d wrote:It's all about: generic function 'lerp()' exists... user supplies new type, user extends standard named function 'lerp()' for their new type. We do this sort of things a lot. Consider std.conv.to?On 3 September 2016 at 11:38, Andrei Alexandrescu via Digitalmars-d <digitalmars-d puremagic.com> wrote:Use static if inside the function. The entire notion of "call this function if you can't find something somewhere that works" is questionable. -- AndreiOn 9/3/16 2:41 AM, Manu via Digitalmars-d wrote:Then we need a decent way to do this.On 3 September 2016 at 00:18, Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:We're better off without that. -- AndreiIn the past, I have suggested using the "default" keyword to specify a fallback function of this kind. I think it's a useful pattern for generic algorithms that have optimized variants on specific types for performance. void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) default {}It's an interesting idea... flesh out a DIP?
Sep 03 2016
On 9/3/16 6:10 PM, Manu via Digitalmars-d wrote:We do this sort of things a lot. Consider std.conv.to?std.conv.to is not a frequently done thing. -- Andrei
Sep 03 2016
Am Sat, 3 Sep 2016 14:24:13 +0200 schrieb Andrei Alexandrescu <SeeWebsiteForEmail erdani.org>:On 9/3/16 7:29 AM, Manu via Digitalmars-d wrote:This notion is what drives template specializations in D: char foo(T)(T t) { return '?'; } // Call this if nothing else matches char foo(T : int)(T t) { return 'i'; } // Call this for ints char foo(T : string)(T t) { return 's'; } // Call this for strings Granted it's a bit more fuzzy and talks about "more specialized", but it is ultimately the same as a hard fallback when foo(3.4) is called. UFCS functions are also only invoked if there is no method with that name on the type, or any type reachable through "alias this" or opDot and there is no opDispatch on said types accepting that name. However questionable the notion is, it is common in D today. Static if also wont work in cases where the signature needs to be different, like overloads of opCast, where one returns bool and is const and another returns a different view on the same thing by ref and is inout. opCast(T:bool) would be unclean, as it also matches types T with a boolean 'alias this'. One can also imagine that some overloads want to take their arguments by value while others take it by ref. -- MarcoOn 3 September 2016 at 11:38, Andrei Alexandrescu via Digitalmars-d <digitalmars-d puremagic.com> wrote:Use static if inside the function. The entire notion of "call this function if you can't find something somewhere that works" is questionable. -- AndreiOn 9/3/16 2:41 AM, Manu via Digitalmars-d wrote:Then we need a decent way to do this.On 3 September 2016 at 00:18, Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:We're better off without that. -- AndreiIn the past, I have suggested using the "default" keyword to specify a fallback function of this kind. I think it's a useful pattern for generic algorithms that have optimized variants on specific types for performance. void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) default {}It's an interesting idea... flesh out a DIP?
Sep 03 2016
On 8/31/2016 10:37 PM, Manu via Digitalmars-d wrote:So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D?import core.stdc.stdio; void f(T:T)(T t) if(is(T == int)) { printf("case int\n"); } void f(T:T)(T t) if(is(T == uint)) { printf("case uint\n"); } void f(T)(T t) { printf("case default\n"); } void main() { f(1); f(1u); f(1.0); } ---- A bit odd, but far better than SFINAE.
Sep 03 2016
On 3 September 2016 at 19:19, Walter Bright via Digitalmars-d <digitalmars-d puremagic.com> wrote:On 8/31/2016 10:37 PM, Manu via Digitalmars-d wrote:This is interesting. Can you explain how that works?So, consider a set of overloads: void f(T)(T t) if(isSomething!T) {} void f(T)(T t) if(isSomethingElse!T) {} void f(T)(T t) {} I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D?import core.stdc.stdio; void f(T:T)(T t) if(is(T == int)) { printf("case int\n"); } void f(T:T)(T t) if(is(T == uint)) { printf("case uint\n"); } void f(T)(T t) { printf("case default\n"); } void main() { f(1); f(1u); f(1.0); } ---- A bit odd, but far better than SFINAE.
Sep 03 2016
On 9/3/2016 2:43 AM, Manu via Digitalmars-d wrote:This is interesting. Can you explain how that works?Specializations are preferred over non-specializations, and T:T is the identity specialization.
Sep 03 2016
On 2016-09-03 11:56, Walter Bright wrote:Specializations are preferred over non-specializations, and T:T is the identity specialization.What if the compiler can prefer template constraint in the same way? Then perhaps this workaround could be avoided. -- /Jacob Carlborg
Sep 03 2016
On 9/3/2016 4:22 AM, Jacob Carlborg wrote:On 2016-09-03 11:56, Walter Bright wrote:It's extremely risky to invent new template lookup rules for existing code. Who knows whose carefully constructed template ox will get gored by it. The suggested technique is simple, works on all the existing D compilers since constraints, and doesn't break anything. I suggest we move on.Specializations are preferred over non-specializations, and T:T is the identity specialization.What if the compiler can prefer template constraint in the same way? Then perhaps this workaround could be avoided.
Sep 03 2016
Am Sat, 3 Sep 2016 02:56:16 -0700 schrieb Walter Bright <newshound2 digitalmars.com>:On 9/3/2016 2:43 AM, Manu via Digitalmars-d wrote:Pretty cool, I'll try that next time I write templated overload sets! -- MarcoThis is interesting. Can you explain how that works?Specializations are preferred over non-specializations, and T:T is the identity specialization.
Sep 03 2016