On 25.02.2011 17:28, spir wrote:
Hello,
Imagine I have the following custom type:
alias float[] Numbers;
Is it possible to override to!string for Numbers so that it outputs eg
like;
(1.1 2.2 3.3)
?
I can indeed override/specialise toImpl or formatValue for the custom
type, but those overrides are simply ignored (tested).
Denis
First things first, it's just an alias and not a custom type. So
compiler won't distinguish between float[] and Numbers at all.
The obvious way around is make your own thin wrapper, but yeah, it won't
get format specifiers until writeTo or similar proposal implemented.
import std.stdio,std.conv;
struct Numbers{
float[] data;
string toString(){
string result = "(";
foreach(val;data[0..$-1]){
result ~= to!string(val);
result ~= ' ';
}
result ~= to!string(data[$-1]);
result ~= ')';
return result;
}
alias data this;
}
Usage:
void main(){
Numbers nums = Numbers([1.1,2.2,3.3]);
writefln("%s",nums);
nums[0] = 4.4;
writefln("%s",nums);
}
--
Dmitry Olshansky