Extending library functions

tn no at email.com
Thu Oct 18 06:53:51 PDT 2012


On Thursday, 18 October 2012 at 13:35:55 UTC, simendsjo wrote:
> On Thursday, 18 October 2012 at 12:10:17 UTC, tn wrote:
>> On Thursday, 18 October 2012 at 11:43:40 UTC, simendsjo wrote:
>>> On Thursday, 18 October 2012 at 11:31:47 UTC, tn wrote:
>>> (...)
>>> You need to manually add std.math.exp2 to the overload set so 
>>> importing external methods doesn't hijack your methods:
>>> http://dlang.org/function.html#overload-sets
>>>
>>> alias std.math.exp2 exp2;
>>
>> Thanks, that clarifies quite a lot. Unfortunately my example 
>> was too simplified, as my type is a template.
>>
>> This still does not work:
>> --------------------
>> import std.math;
>>
>> struct Lognum(T) {
>> 	T lx;
>> }
>>
>> T log(T)(Lognum!T x) {
>> 	return x.lx;
>> }
>>
>> alias std.math.log log;
>>
>> void main() {
>> 	//assert(std.math.log(1.0) == 0.0);
>> 	assert(log(1.0) == 0.0);
>> 	Lognum!double x;
>> 	x.lx = 0.0;
>> 	assert(log(x) == 0.0);
>> }
>> --------------------
>
> I don't think you can overload template methods with 
> non-template methods:
>
> void f(string i) {}
> void f(T)(T i) if (is(T == double)) {}
>
> void main(string[] args) {
>     f(2.2);
> }
>
> Error: template ol.f(T) if (is(T == double)) conflicts with 
> function ol.f at ol.d(1)

That's too bad. But why then does this work:

--------------------
module a;
void f(string i) {}
--------------------
module b;
void f(T)(T i) if (is(T == double)) {}
--------------------
import a;
import b;

void main(string[] args) {
     f("asdf");
     f(2.2);
}
--------------------


More information about the Digitalmars-d-learn mailing list