Re: Why is it not possible to write to a file from a const member function ?

2016-03-12 Thread user42 via Digitalmars-d-learn
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

Re: Why is it not possible to write to a file from a const member function ?

2016-03-12 Thread Mike Parker via Digitalmars-d-learn
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

Why is it not possible to write to a file from a const member function ?

2016-03-12 Thread user42 via Digitalmars-d-learn
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

Re: const member function

2015-02-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, February 23, 2015 09:12:33 rumbu via Digitalmars-d-learn wrote: > On Saturday, 21 February 2015 at 15:26:28 UTC, ketmar wrote: > > On Sat, 21 Feb 2015 08:27:13 +, rumbu wrote: > > > >> My question was not how I do this, I know already. My question > >> was if > >> there is another wa

Re: const member function

2015-02-23 Thread ketmar via Digitalmars-d-learn
On Mon, 23 Feb 2015 09:12:33 +, rumbu wrote: > On Saturday, 21 February 2015 at 15:26:28 UTC, ketmar wrote: >> On Sat, 21 Feb 2015 08:27:13 +, rumbu wrote: >> >>> My question was not how I do this, I know already. My question was if >>> there is another way to safely call a non-const insta

Re: const member function

2015-02-23 Thread rumbu via Digitalmars-d-learn
On Saturday, 21 February 2015 at 15:26:28 UTC, ketmar wrote: On Sat, 21 Feb 2015 08:27:13 +, rumbu wrote: My question was not how I do this, I know already. My question was if there is another way to safely call a non-const instance function on a const object. is there a way to been saf

Re: const member function

2015-02-21 Thread Foo via Digitalmars-d-learn
On Saturday, 21 February 2015 at 15:26:28 UTC, ketmar wrote: On Sat, 21 Feb 2015 08:27:13 +, rumbu wrote: My question was not how I do this, I know already. My question was if there is another way to safely call a non-const instance function on a const object. is there a way to been saf

Re: const member function

2015-02-21 Thread Foo via Digitalmars-d-learn
On Saturday, 21 February 2015 at 06:38:18 UTC, rumbu wrote: Often I'm using the following code pattern: class S { private SomeType cache; public SomeType SomeProp() @property { if (cache is null) cache = SomeExpensiveOperation(); return cache; } } Is there any w

Re: const member function

2015-02-21 Thread ketmar via Digitalmars-d-learn
On Sat, 21 Feb 2015 08:27:13 +, rumbu wrote: > My question was not how I do this, I know already. My question was if > there is another way to safely call a non-const instance function on a > const object. is there a way to been safely hit by a truck? signature.asc Description: PGP signature

Re: const member function

2015-02-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, February 21, 2015 08:27:13 rumbu via Digitalmars-d-learn wrote: > On Saturday, 21 February 2015 at 08:08:25 UTC, Mike Parker wrote: > > On 2/21/2015 4:31 PM, rumbu wrote: > > > > you can do this instead: > > > > this() { cache = SomeExpensiveOp(); } > > > > public @property con

Re: const member function

2015-02-21 Thread Baz via Digitalmars-d-learn
On Saturday, 21 February 2015 at 07:31:19 UTC, rumbu wrote: On Saturday, 21 February 2015 at 07:01:12 UTC, Baz wrote: --- class S { private SomeType cache; public const(SomeType) SomeProp() @property { if (cache is null) cache = SomeExpensiveOperation(); return cach

Re: const member function

2015-02-21 Thread rumbu via Digitalmars-d-learn
On Saturday, 21 February 2015 at 08:08:25 UTC, Mike Parker wrote: On 2/21/2015 4:31 PM, rumbu wrote: you can do this instead: this() { cache = SomeExpensiveOp(); } public @property const(int) SomeProp() const { return cache; } Notice the const on the end of SomeProp. T

Re: const member function

2015-02-21 Thread Mike Parker via Digitalmars-d-learn
On 2/21/2015 4:31 PM, rumbu wrote: My intention is not to have a read-only getter, I want to call SomeProp on a const object: class S { private int cache = -1; private int SomeExpensiveOp() { return 12345; } public @property const(int) SomeProp() { if (cache = -1)

Re: const member function

2015-02-20 Thread rumbu via Digitalmars-d-learn
On Saturday, 21 February 2015 at 07:01:12 UTC, Baz wrote: --- class S { private SomeType cache; public const(SomeType) SomeProp() @property { if (cache is null) cache = SomeExpensiveOperation(); return cache; } } --- the result of the getter will be read-o

Re: const member function

2015-02-20 Thread Baz via Digitalmars-d-learn
--- class S { private SomeType cache; public const(SomeType) SomeProp() @property { if (cache is null) cache = SomeExpensiveOperation(); return cache; } } --- the result of the getter will be read-only

const member function

2015-02-20 Thread rumbu via Digitalmars-d-learn
Often I'm using the following code pattern: class S { private SomeType cache; public SomeType SomeProp() @property { if (cache is null) cache = SomeExpensiveOperation(); return cache; } } Is there any way to mark SomeProp() as const? Because I want to call somew