Strange behavior with opAssign and static if

Andre andre at s-e-a-p.de
Sat Feb 8 02:35:28 PST 2014


Hi,

I have a template structure Decimal which is used in a OrderDb 
structure. The opAssign method in Decimal leads to strange
behavior in the function execute();

The function execute stores the values of OrderDb in a variant
array. Decimals value should be stored as strings. There is
a static if for this condition.

There is a compilation error for the line
arr[i] = __traits(getMember, struc, member);

-> function.main.OrderDb.opAssign (OrderDb p) is not callable using 
argument types ()

This compilation error disappears if I remove the opAssign method
in Decimal.

How can the opAssign of Decimal influence the code path for a non
decimal field?

Kind regards
André

---code---

import std.traits: isInstanceOf;
import std.variant;

struct Decimal(int precision, int scale){
	void opAssign(T)(T d)
		if (isInstanceOf!(Decimal, T)){}

	string toString(){return "10.22";};
}

struct OrderDb{
	string orderId;
	Decimal!(10,2) amount;
}

void main(){
	execute(OrderDb());
}

void execute(T)(T struc){
	Variant[] arr = new Variant[](2);

	foreach (i, member;__traits(allMembers, T)){
		static if (isInstanceOf!(Decimal, typeof(__traits(getMember, struc, 
member)))){
			// Store decimals as string
			arr[i] = (__traits(getMember, struc, member)).toString();
		} else {
			// Store other values with their types
			arr[i] = __traits(getMember, struc, member);
		}
	}
}


More information about the Digitalmars-d-learn mailing list