September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | 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.
|
September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 03.09.2016 05:15, Walter Bright wrote: > 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. > It had already been reported: https://issues.dlang.org/show_bug.cgi?id=11856 I have added this example to the report. |
September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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!
|
September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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:
>>
>> 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.
This is interesting. Can you explain how that works?
|
September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | 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.
|
September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 |
September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On 9/3/2016 4:22 AM, Jacob Carlborg wrote:
> 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.
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.
|
September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | 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:
>> 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:
>>>>
>>>>
>>>> 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?
>>
>>
>> We're better off without that. -- Andrei
>
> Then we need a decent way to do this.
Use static if inside the function. The entire notion of "call this function if you can't find something somewhere that works" is questionable. -- Andrei
|
September 04, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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:
>>
>> 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:
>>>>
>>>>
>>>> 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?
>>>
>>>
>>>
>>> We're better off without that. -- Andrei
>>
>>
>> Then we need a decent way to do this.
>
>
> Use static if inside the function. The entire notion of "call this function if you can't find something somewhere that works" is questionable. -- Andrei
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?
|
September 03, 2016 Re: Fallback 'catch-all' template functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | 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
|
Copyright © 1999-2021 by the D Language Foundation