| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
May 02, 2014 formattedWrite writes nothing | ||||
|---|---|---|---|---|
| ||||
class MyClass {
Appender!string _stringBuilder;
this() {
_stringBuilder = Appender!string(null);
_stringBuilder.clear();
}
@property string str() {
return _stringBuilder.data;
}
void append(string s) {
formattedWrite(_stringBuilder, "%s", s);
}
}
MyClass c = new MyClass();
c.append("text 1");
c.append("__222");
writeln(c.str); //in this case nothing is printed out
Following workarounds work:
1) call _stringBuilder.put() instead of formattedWrite()
2) if "_stringBuilder.clear()" is omitted in the constructor, formattedWrite(...) will work as expected.
Is it a bug or is there a reason for such behaviour?
| ||||
May 02, 2014 Re: formattedWrite writes nothing | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ref2401 | On 5/2/14, ref2401 via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > class MyClass { > Appender!string _stringBuilder; > > this() { > _stringBuilder = Appender!string(null); > _stringBuilder.clear(); Ouch, ouch, ouch! What's happening is that the 'clear' Appender method is only compiled-in if the data is mutable, otherwise you end up calling the object.clear UFCS function. So don't use clear here. I don't know if this is a case of poor method naming or another downside of UFCS. Luckily 'clear' is being renamed to 'destroy' in the object module, so this specific case will not become a problem in the future. | |||
May 02, 2014 Re: formattedWrite writes nothing | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Friday, 2 May 2014 at 10:23:03 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote:
> Ouch, ouch, ouch! What's happening is that the 'clear' Appender method
> is only compiled-in if the data is mutable, otherwise you end up
> calling the object.clear UFCS function. So don't use clear here.
>
> I don't know if this is a case of poor method naming or another
> downside of UFCS. Luckily 'clear' is being renamed to 'destroy' in the
> object module, so this specific case will not become a problem in the
> future.
I'd say clear should be @disabled in Appender for non-mutable data.
| |||
May 02, 2014 Re: formattedWrite writes nothing | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ref2401 | I logged this bug a while ago: https://issues.dlang.org/show_bug.cgi?id=10291 On Friday, 2 May 2014 at 10:06:43 UTC, ref2401 wrote: > > Is it a bug or is there a reason for such behaviour? | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply