Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

generic toUtfx / formatting possible?

Moderators: larsivi kris

Posted: 06/17/07 09:56:21

I have a templated class which needs some printing/serialization routines. But it's base types can be classes (which have their toUtfx routines) or basic integer types like int or long. Is there a way to use printing functions for those base types in a generic way?


complains: "Error: no property 'toUtf8' for type 'int'" if I instantiate this template class for int. Or is there at least a way to determine (via static if maybe?), if the type is a class or native data type?

Author Message

Posted: 06/17/07 10:02:27

I just realized that this is somehow possible for Stdout via

Stdout (obj);

But I need the resulting string, not printed to console! Can one attach a Print!(char) to a char[] in some way?

Posted: 06/17/07 10:21:53 -- Modified: 06/17/07 13:56:37 by

I'll answer both your question's here - if you need to check compile time whether it is basic types, or classes, try tango.core.Traits.

As for getting the char[] from Print (or even Stdout if anyway are using Stdout for something):

auto mystring = Stdout.layout.convert("Formatstring", arg1, arg2);

Same goes for Print, which also has the layout instance (actually, as you may know, Stdout is just a Print instance). Layout also has sprint methods, which takes an input buffer for the result and which the return value is sliced from.

Posted: 06/20/07 18:02:49

Thank you - this works like a charm. Just wanted to ask, whether it makes sense to let each class have it's own Layout instance. (Or whether it's performance/memory critical) Or is there a global one to use? Okay, there's the one from Stdout/Stderr, but this seems to me somehow unasthetic :)

Posted: 06/20/07 19:53:36

It's quite ok to do so, and you can instantiate using one of the other character classes, or subclass it like is done with Locale. There is no global instance, but it shouldn't pose any problems to create on yourself.