www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - format()

reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
Apparently std.string.format() is not implemented / does not compile! :(
Is there any sort of replacement?
Something which works like writefln() but output a string!
Jun 12 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Lloyd Dupon:

 Apparently std.string.format() is not implemented / does not compile! :(
This works for me, DMD 2.053: import std.stdio, std.string; void main() { int x = 10; auto s = format("%d", 10); writeln(">", s, "<"); } Bye, bearophile
Jun 12 2011
parent reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
mm... ok.
but why the line below doesn't compile?

mixin(format("class %s {}", "A"));



"bearophile"  wrote in message news:it2pf5$1qh6$1 digitalmars.com... 
 Apparently std.string.format() is not implemented / does not compile! :(
This works for me, DMD 2.053:
Jun 12 2011
next sibling parent reply David Nadlinger <see klickverbot.at> writes:
On 6/12/11 6:37 PM, Lloyd Dupont wrote:
 mm... ok.
 but why the line below doesn't compile?

 mixin(format("class %s {}", "A"));
Because format presumably can't be interpreted at compile time (yet) – not all functions are necessarily CTFEable. David
Jun 12 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-06-12 10:30, David Nadlinger wrote:
 On 6/12/11 6:37 PM, Lloyd Dupont wrote:
 mm... ok.
 but why the line below doesn't compile?
=20
 mixin(format("class %s {}", "A"));
=20 Because format presumably can't be interpreted at compile time (yet) =E2=
=80=93
 not all functions are necessarily CTFEable.
Yeah. format can only be used at runtime. If you want a version which works= at=20 compile time, then you std.metastrings.Format, which is an eponymous templa= te.=20 e.g. mixin(Format!("class %s {}", "A")); should work. Of course, in this particular case, you might as well just giv= e=20 the whole string to the mixin directly, but I assume that the example is so= =20 simple simply because it's an example. =2D Jonathan M Davis
Jun 12 2011
parent "Lloyd Dupont" <ld-REMOVE galador.net> writes:
yep, the example is simple because it is an example!
Thanks for your suggestion! :)

"Jonathan M Davis"  wrote in message 
news:mailman.850.1307909499.14074.digitalmars-d-learn puremagic.com...

On 2011-06-12 10:30, David Nadlinger wrote:
 On 6/12/11 6:37 PM, Lloyd Dupont wrote:
 mm... ok.
 but why the line below doesn't compile?

 mixin(format("class %s {}", "A"));
Because format presumably can't be interpreted at compile time (yet) – not all functions are necessarily CTFEable.
Yeah. format can only be used at runtime. If you want a version which works at compile time, then you std.metastrings.Format, which is an eponymous template. e.g. mixin(Format!("class %s {}", "A")); should work. Of course, in this particular case, you might as well just give the whole string to the mixin directly, but I assume that the example is so simple simply because it's an example. - Jonathan M Davis
Jun 12 2011
prev sibling parent jdrewsen <jdrewsen nospam.com> writes:
Den 12-06-2011 18:37, Lloyd Dupont skrev:
 mm... ok.
 but why the line below doesn't compile?

 mixin(format("class %s {}", "A"));
Because the mixin is evaluated at compile time. This means that format(...) is evaluated at compile time which afaik is not supported. It may be supported in the future with the improved CTFE that is being worked on. /Jonas
Jun 12 2011