Re: Filling an array
On Saturday, 12 March 2016 at 18:33:16 UTC, Alex wrote: On Saturday, 12 March 2016 at 16:37:25 UTC, user42 wrote: On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote: /snip I thought this was supposed to halt with an error rather than compile and set all members to 1. The syntax, to me anyways, doesn't really communicate the intention of: set all members to 1. //arr[] = 1; Whereas the following does fill(arr, 1); Well, this was not the question. As stated here: https://dlang.org/spec/arrays.html in the section "array setting", it is possible to set an array in such a manner. And my question was, why a specific array behaves not as expected. So, either there is a problem with filling an array, or, there is a problem with implicit conversion of a Nullable!T to its underlying type. Learned something new. I guess I missed that detail when I read that page.
Re: Filling an array
On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote: /snip I thought this was supposed to halt with an error rather than compile and set all members to 1. The syntax, to me anyways, doesn't really communicate the intention of: set all members to 1. //arr[] = 1; Whereas the following does fill(arr, 1);
Re: Why is it not possible to write to a file from a const member function ?
On Saturday, 12 March 2016 at 15:32:39 UTC, Mike Parker wrote: On Saturday, 12 March 2016 at 14:02:31 UTC, user42 wrote: Why is this thing not compiling ? Or, in other words, how is is possible to log something to a file from a const member function ? Const member functions functions are not allowed to mutate any member state at all. This includes the state of any object instances that are members. Since the write method of the File type is not declared as const, then you can not call it on a member of type File from inside a const member function. Logging, by its very definition, mutates state. Move the File instance outside of the class and it works. import std.stdio; private File f; static this() { f = stdout; } class X { void p(string s) const { f.writeln!(string)(s); } } class Y { private string s = "Y"; override string toString() const { return s; } } void main() { auto x = new X; auto y = new Y; import std.conv: to; x.p(to!string(y)); } Thanks for your reply. Unfortunately it only solves the problem for this particular snippet. The most interesting part of your reply is this line: Since the write method of the File type is not declared as const, At a quick glance I suppose it's because of the locking in LockingTextWriter. I think I will probably pass this stuff to a C implementation, or override toString non-const, since adding const to it started this const avalanche in the first place. Anyways, thanks for your input and have a nice weekend.
Why is it not possible to write to a file from a const member function ?
Hi I have the following snippet to illustrate my problem/question: class X { import std.stdio: write, File, stdout; private File* f = &stdout; void p(string s) const { f.write(s); } } class Y { private string s = "Y"; override string toString() const { return s; } } void main() { auto x = new X; auto y = new Y; import std.conv: to; x.p(to!string(y)); } Why is this thing not compiling ? Or, in other words, how is is possible to log something to a file from a const member function ? Thanks in advance