Thread overview | |||||
---|---|---|---|---|---|
|
September 02, 2014 Get operator overloads | ||||
---|---|---|---|---|
| ||||
Is it possible to get all overloads of an operator for a particular type? I.e., having this struct definition, is it possible to tell at compile time that it can be added with double and int[]? struct S { void opBinary(string op : "+")(double); void opBinary(string op : "+")(int[]); } To clarify, I am well aware that it can also be overloaded through the second argument via opBinaryRight, but for now I'm only interested in overloads provided by S itself. I've tried __traits(getOverloads), but with no success: __traits(getOverloads, S, "opBinary") returns empty tuple, and __traits(getOverloads, S, `opBinary!"+"`) results in a compilation error (dmd 2.065): Error: no property 'opBinary!"+"' for type 'S' Error: (S).opBinary!"+" cannot be resolved |
September 02, 2014 Re: Get operator overloads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Timokhin | On Tuesday, 2 September 2014 at 18:55:50 UTC, Ivan Timokhin wrote:
> Is it possible to get all overloads of an operator for a particular type?
>
> I.e., having this struct definition, is it possible to tell at compile time that it can be added with double and int[]?
>
> struct S
> {
> void opBinary(string op : "+")(double);
> void opBinary(string op : "+")(int[]);
> }
>
> To clarify, I am well aware that it can also be overloaded through the second argument via opBinaryRight, but for now I'm only interested in overloads provided by S itself.
>
> I've tried __traits(getOverloads), but with no success: __traits(getOverloads, S, "opBinary") returns empty tuple,
> and __traits(getOverloads, S, `opBinary!"+"`) results in a compilation
> error (dmd 2.065):
>
> Error: no property 'opBinary!"+"' for type 'S'
> Error: (S).opBinary!"+" cannot be resolved
I can't answer the question about inspecting the overloads, but if you just want to know whether it supports addition, the idiomatic way is to test whether it compiles:
enum supportsAddition(S) = is(typeof(S.init + 0.0));
static if(supportsAddition!MyType) { ... }
|
September 03, 2014 Re: Get operator overloads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 02.09.2014 23:21, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm@gmx.net>" пишет:
> On Tuesday, 2 September 2014 at 18:55:50 UTC, Ivan Timokhin wrote:
>> Is it possible to get all overloads of an operator for a particular type?
>>
>> I.e., having this struct definition, is it possible to tell at compile
>> time that it can be added with double and int[]?
>>
>> struct S
>> {
>> void opBinary(string op : "+")(double);
>> void opBinary(string op : "+")(int[]);
>> }
>>
>> To clarify, I am well aware that it can also be overloaded through the
>> second argument via opBinaryRight, but for now I'm only interested in
>> overloads provided by S itself.
>>
>> I've tried __traits(getOverloads), but with no success:
>> __traits(getOverloads, S, "opBinary") returns empty tuple,
>> and __traits(getOverloads, S, `opBinary!"+"`) results in a compilation
>> error (dmd 2.065):
>>
>> Error: no property 'opBinary!"+"' for type 'S'
>> Error: (S).opBinary!"+" cannot be resolved
>
> I can't answer the question about inspecting the overloads, but if you
> just want to know whether it supports addition, the idiomatic way is to
> test whether it compiles:
>
> enum supportsAddition(S) = is(typeof(S.init + 0.0));
>
> static if(supportsAddition!MyType) { ... }
Thanks for the answer, but I what I want is not to know whether it supports addition with some particular type, but to list all types that are supported (at least explicitly, ignoring templates like opBinary(string op, T)(T)).
|
Copyright © 1999-2021 by the D Language Foundation