Extending library functions

tn no at email.com
Thu Oct 18 05:10:16 PDT 2012


On Thursday, 18 October 2012 at 11:43:40 UTC, simendsjo wrote:
> On Thursday, 18 October 2012 at 11:31:47 UTC, tn wrote:
>> Hi.
>>
>> I want to extend math library functions to work with my own 
>> type. However, the definition for my own type seems to prevent 
>> automated access to the original function. How can I fix this 
>> unexpected behavior?
>>
>> Simplified example:
>> --------------------
>> import std.math;
>>
>> int exp2(int x) {
>>    return 1 >> x;
>> }
>>
>> void main() {
>>    assert(exp2(0.0) == 1.0);              // <= why does not 
>> this work anymore?
>>    //assert(std.math.exp2(0.0) == 1.0);   // <= this works
>> }
>> --------------------
>
> 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);
}
--------------------



More information about the Digitalmars-d-learn mailing list