digitalmars.D.learn - Get operator overloads
- Ivan Timokhin (17/17) Sep 02 2014 Is it possible to get all overloads of an operator for a particular type...
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (6/25) Sep 02 2014 I can't answer the question about inspecting the overloads, but
- Ivan Timokhin (5/33) Sep 03 2014 Thanks for the answer, but I what I want is not to know whether it
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
Sep 02 2014
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 resolvedI 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) { ... }
Sep 02 2014
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: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)).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 resolvedI 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) { ... }
Sep 03 2014