Re: cannot modify struct with immutable members

2015-01-03 Thread ted via Digitalmars-d-learn
Ali Çehreli wrote: > On 01/02/2015 10:10 PM, ted wrote: > > > I'm now taking the view that const is there for the compiler to > > optimise code on the basis that nothing can alter it once set (and can > > only be set on initialisation). > > Of course, that is true for const values, not for co

Re: cannot modify struct with immutable members

2015-01-03 Thread ketmar via Digitalmars-d-learn
On Sat, 03 Jan 2015 01:00:58 -0800 Ali Çehreli via Digitalmars-d-learn wrote: > On 01/02/2015 09:07 PM, ketmar via Digitalmars-d-learn wrote: > > > structure instance with const fields can be initialized only once, upon > > creation. so did `Test myTest1;` -- you initialized `myTest1` with >

Re: cannot modify struct with immutable members

2015-01-03 Thread Ali Çehreli via Digitalmars-d-learn
On 01/02/2015 10:10 PM, ted wrote: > I'm now taking the view that const is there for the compiler to optimise > code on the basis that nothing can alter it once set (and can only be set > on initialisation). Of course, that is true for const values, not for const references. In the latter case

Re: cannot modify struct with immutable members

2015-01-03 Thread Ali Çehreli via Digitalmars-d-learn
On 01/02/2015 09:07 PM, ketmar via Digitalmars-d-learn wrote: > structure instance with const fields can be initialized only once, upon > creation. so did `Test myTest1;` -- you initialized `myTest1` with > default values. you can't reinitialize it later. > > in C++ constness on member doesn't im

Re: cannot modify struct with immutable members

2015-01-02 Thread ketmar via Digitalmars-d-learn
On Sat, 03 Jan 2015 16:40:14 +1030 ted via Digitalmars-d-learn wrote: > I'm now taking the view that const is there for the compiler to optimise > code on the basis that nothing can alter it once set (and can only be set > on initialisation). So I see your point that it would not be used very

Re: cannot modify struct with immutable members

2015-01-02 Thread ted via Digitalmars-d-learn
ketmar via Digitalmars-d-learn wrote: > On Sat, 03 Jan 2015 15:56:58 +1030 > ted via Digitalmars-d-learn wrote: > >> Ironically, I'm trying to use const in an effort to understand it...but >> there seems to be an unusual amount of pain until I grok it. > just remember that `const` "infects" ever

Re: cannot modify struct with immutable members

2015-01-02 Thread ketmar via Digitalmars-d-learn
On Sat, 03 Jan 2015 15:56:58 +1030 ted via Digitalmars-d-learn wrote: > Ironically, I'm trying to use const in an effort to understand it...but > there seems to be an unusual amount of pain until I grok it. just remember that `const` "infects" everything down to the bytes when it's applied. and

Re: cannot modify struct with immutable members

2015-01-02 Thread ted via Digitalmars-d-learn
ketmar via Digitalmars-d-learn wrote: > On Sat, 03 Jan 2015 14:45:24 +1030 > ted via Digitalmars-d-learn wrote: > >> Well, I just cleared up some of my misunderstanding. >> >> I did not realise the mA (within struct Test) would be a _copy_ of arg, >> not a reference (pointer) to arg. >> >> So

Re: cannot modify struct with immutable members

2015-01-02 Thread ketmar via Digitalmars-d-learn
On Sat, 03 Jan 2015 14:45:24 +1030 ted via Digitalmars-d-learn wrote: p.s. also please note that structs in D are always passed by value and copied (until you not explicitly ask for something another). so: MyStruct a; MyStruct b; b = a; actually does `memcpy()` (with postblit if there is

Re: cannot modify struct with immutable members

2015-01-02 Thread ketmar via Digitalmars-d-learn
On Sat, 03 Jan 2015 14:45:24 +1030 ted via Digitalmars-d-learn wrote: > Well, I just cleared up some of my misunderstanding. > > I did not realise the mA (within struct Test) would be a _copy_ of arg, not > a reference (pointer) to arg. > > So the more correct code snippet would be: > > stru

Re: cannot modify struct with immutable members

2015-01-02 Thread ted via Digitalmars-d-learn
Well, I just cleared up some of my misunderstanding. I did not realise the mA (within struct Test) would be a _copy_ of arg, not a reference (pointer) to arg. So the more correct code snippet would be: struct A { int someInt; } struct Test { @property { const(A) getA() { return *mA;

Re: cannot modify struct with immutable members

2015-01-02 Thread ted via Digitalmars-d-learn
Hi, thanks for the reply...to answer your direct question, the original code looked like: struct A { int someInt; } struct Test { @property { const(A) getA() { return mA; } } this( ref const(A) arg ) { mA = arg; } private: A mA; } void main() { Test myTest

Re: cannot modify struct with immutable members

2015-01-02 Thread ketmar via Digitalmars-d-learn
On Sat, 03 Jan 2015 13:25:31 +1030 ted via Digitalmars-d-learn wrote: > > I get the following error from the code below: (dmd2.066.1, linux) > test.d(26): Error: cannot modify struct myTest1 Test with immutable members > > Is this expected? > > If so, how can I achieve this result - being able

cannot modify struct with immutable members

2015-01-02 Thread ted via Digitalmars-d-learn
I get the following error from the code below: (dmd2.066.1, linux) test.d(26): Error: cannot modify struct myTest1 Test with immutable members Is this expected? If so, how can I achieve this result - being able to set (a new) initial value of myTest1 from within an nested function ? thanks ! t