Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 12, 2011 format() | ||||
---|---|---|---|---|
| ||||
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! |
June 12, 2011 Re: format() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lloyd Dupont | 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
|
June 12, 2011 Re: format() | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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: |
June 12, 2011 Re: format() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lloyd Dupont | 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
|
June 12, 2011 Re: format() | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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
|
June 12, 2011 Re: format() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lloyd Dupont | 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
|
June 13, 2011 Re: format() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 |
Copyright © 1999-2021 by the D Language Foundation