On 01.12.2010 20:11, Matthias Pleh wrote:
Am 01.12.2010 16:51, schrieb vincent picaud:
Is there a canonical way to take into account a new type for I/O
using the std phobos library ?
To be clear I do not know how to translate something like this (in
C++) in D :
#include<iostream>
class A {};
std::ostream& operator<<(std::ostream& out,const A& a)
{
out<< "\nscreen output routine here\n";
return out;
}
int main()
{
A a;
std::cout<< a;
}
Do I have to overload some writeln functions ?
any help is welcome :)
I would make it this way:
module test;
import std.stdio;
class A{}
class B{ string toString() {return "screen output routine here";}}
int main(string[] args)
{
A a=new A;
B b=new B;
writeln(a.toString());
writeln(b.toString());
Or even more implicit version, since writeln and the likes recognize
toString:
writeln(a);
writeln(b);
return 0;
}
output:
> test.A
> screen output routine here
Base-class of all classes is object with have a default-toString()
method, which gives the module.classname.
So you can overwrite this method.
Well there is a relatively new proposal which aims to replace toString
with more eficient and flexible function (and was generally accepted):
http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP9
--
Dmitry Olshansky