Re: mutable keyword

2016-05-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, May 23, 2016 14:08:43 Jack Applegame via Digitalmars-d-learn wrote: > On Monday, 23 May 2016 at 11:05:34 UTC, Jonathan M Davis wrote: > > I don't know why you think that D violates its own rules > > frequently though. It's not perfect, but it usually does follow > > its own rules - and

Re: mutable keyword

2016-05-23 Thread Jack Applegame via Digitalmars-d-learn
On Monday, 23 May 2016 at 11:05:34 UTC, Jonathan M Davis wrote: I don't know why you think that D violates its own rules frequently though. It's not perfect, but it usually does follow its own rules - and when it doesn't, we fix it (though not always as quickly as would be ideal). The most

Re: mutable keyword

2016-05-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, May 23, 2016 08:19:52 Jack Applegame via Digitalmars-d-learn wrote: > On Sunday, 22 May 2016 at 13:08:19 UTC, Jonathan M Davis wrote: > > Given how const and immutable work in D, having any portion of > > them be treated as mutable, becomes highly problematic. It > > could theoretically

Re: mutable keyword

2016-05-23 Thread Jack Applegame via Digitalmars-d-learn
On Sunday, 22 May 2016 at 13:08:19 UTC, Jonathan M Davis wrote: Given how const and immutable work in D, having any portion of them be treated as mutable, becomes highly problematic. It could theoretically be done by having to mark such variables with an attribute and mark such types with a

Re: mutable keyword

2016-05-22 Thread chmike via Digitalmars-d-learn
validity checking. I had the impression that this is the principle used for the pure keyword. I would vote against the introduction of the mutable keyword as it exist in C++ because it is a half backed solution. First it tells the compiler that this variable is modifiable at any time by any

Re: mutable keyword

2016-05-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, May 22, 2016 09:42:54 Jack Applegame via Digitalmars-d-learn wrote: > On Saturday, 21 May 2016 at 21:49:23 UTC, Jonathan M Davis wrote: > > Rebindable is in kind of a weird grey area. It involves a > > union, not casting, and it's only ever mutating the class > > reference, not the

Re: mutable keyword

2016-05-22 Thread Marc Schütz via Digitalmars-d-learn
On Sunday, 22 May 2016 at 09:42:54 UTC, Jack Applegame wrote: I agree. But I think we need something that allows *logical* const and immutable. Strict binding constness to physical memory constancy is not always necessary and sometimes even harmful. http://wiki.dlang.org/DIP89

Re: mutable keyword

2016-05-22 Thread Jack Applegame via Digitalmars-d-learn
On Saturday, 21 May 2016 at 21:49:23 UTC, Jonathan M Davis wrote: Rebindable is in kind of a weird grey area. It involves a union, not casting, and it's only ever mutating the class reference, not the object itself. Certainly, if it mutated the object, it would be undefined behavior, but it's

Re: mutable keyword

2016-05-21 Thread ag0aep6g via Digitalmars-d-learn
On 05/21/2016 07:15 PM, Jack Applegame wrote: Really? Mutating immutable is UB too, but look at std.typecons.Rebindable. Rebindable uses a union of a mutable and an immutable variant of the type. No access to the mutable union member is provided, and it's never dereferenced by Rebindable.

Re: mutable keyword

2016-05-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, May 21, 2016 17:15:16 Jack Applegame via Digitalmars-d-learn wrote: > On Friday, 20 May 2016 at 20:46:18 UTC, Jonathan M Davis wrote: > > Casting away const and mutating is undefined behavior in D. No > > D program should ever do it. > > Really? Mutating immutable is UB too, but look

Re: mutable keyword

2016-05-21 Thread Jack Applegame via Digitalmars-d-learn
On Friday, 20 May 2016 at 20:46:18 UTC, Jonathan M Davis wrote: Casting away const and mutating is undefined behavior in D. No D program should ever do it. Really? Mutating immutable is UB too, but look at std.typecons.Rebindable.

Re: mutable keyword

2016-05-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, May 21, 2016 07:00:43 ciechowoj via Digitalmars-d-learn wrote: > On Saturday, 21 May 2016 at 00:39:21 UTC, Jonathan M Davis wrote: > > Well, if you actually tried marking functions with pure, you'd > > see pretty fast that this won't work with pure. A function > > that's marked with

Re: mutable keyword

2016-05-21 Thread ciechowoj via Digitalmars-d-learn
On Saturday, 21 May 2016 at 00:39:21 UTC, Jonathan M Davis wrote: Well, if you actually tried marking functions with pure, you'd see pretty fast that this won't work with pure. A function that's marked with pure cannot access any global, mutable state. It can only access what's passed to it

Re: mutable keyword

2016-05-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, May 20, 2016 21:35:27 ciechowoj via Digitalmars-d-learn wrote: > On Friday, 20 May 2016 at 20:45:05 UTC, Jonathan M Davis wrote: > > If you want something that's ref-counted and works in pure > > code, const will _not_ work, because you can't legally alter > > the ref-count. > > What

Re: mutable keyword

2016-05-20 Thread ciechowoj via Digitalmars-d-learn
On Friday, 20 May 2016 at 20:45:05 UTC, Jonathan M Davis wrote: If you want something that's ref-counted and works in pure code, const will _not_ work, because you can't legally alter the ref-count. What about something like this (ignoring multi-threading issues): struct RefCountPool {

Re: mutable keyword

2016-05-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, May 20, 2016 19:33:49 Johan Engelen via Digitalmars-d-learn wrote: > On Thursday, 19 May 2016 at 23:21:14 UTC, Jonathan M Davis wrote: > > No. D's const and immutable provide no backdoors. Rather, they > > provide strong guarantees. So, if a variable is const, then it > > cannot be

Re: mutable keyword

2016-05-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, May 20, 2016 18:23:26 Jack Applegame via Digitalmars-d-learn wrote: > On Friday, 20 May 2016 at 17:28:55 UTC, Namespace wrote: > > But you can cheat: > You can just cast const away: > struct A { > int id = 0; > > this(int id) { > this.id = id; > } > > void change() const {

Re: mutable keyword

2016-05-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, May 20, 2016 18:41:04 ciechowoj via Digitalmars-d-learn wrote: > On Thursday, 19 May 2016 at 23:21:14 UTC, Jonathan M Davis wrote: > > On Thursday, May 19, 2016 20:44:54 ciechowoj via > > > > Digitalmars-d-learn wrote: > >> Is there D equivalent of C++'s

Re: mutable keyword

2016-05-20 Thread Johan Engelen via Digitalmars-d-learn
On Thursday, 19 May 2016 at 23:21:14 UTC, Jonathan M Davis wrote: No. D's const and immutable provide no backdoors. Rather, they provide strong guarantees. So, if a variable is const, then it cannot be mutated (even internally) except via a mutable reference to the same data. The "even

Re: mutable keyword

2016-05-20 Thread Namespace via Digitalmars-d-learn
On Friday, 20 May 2016 at 18:42:44 UTC, Ali Çehreli wrote: On 05/20/2016 10:28 AM, Namespace wrote: > On Thursday, 19 May 2016 at 23:21:14 UTC, Jonathan M Davis wrote: >> On Thursday, May 19, 2016 20:44:54 ciechowoj via Digitalmars-d-learn >> wrote: >>> Is there D equ

Re: mutable keyword

2016-05-20 Thread ciechowoj via Digitalmars-d-learn
On Friday, 20 May 2016 at 18:23:26 UTC, Jack Applegame wrote: On Friday, 20 May 2016 at 17:28:55 UTC, Namespace wrote: But you can cheat: You can just cast const away: struct A { int id = 0; this(int id) { this.id = id; } void

Re: mutable keyword

2016-05-20 Thread Ali Çehreli via Digitalmars-d-learn
On 05/20/2016 10:28 AM, Namespace wrote: > On Thursday, 19 May 2016 at 23:21:14 UTC, Jonathan M Davis wrote: >> On Thursday, May 19, 2016 20:44:54 ciechowoj via Digitalmars-d-learn >> wrote: >>> Is there D equivalent of C++'s mutable keyword? Like the one that >

Re: mutable keyword

2016-05-20 Thread ciechowoj via Digitalmars-d-learn
On Thursday, 19 May 2016 at 23:21:14 UTC, Jonathan M Davis wrote: On Thursday, May 19, 2016 20:44:54 ciechowoj via Digitalmars-d-learn wrote: Is there D equivalent of C++'s mutable keyword? Like the one that allows to modify a field of struct from constant method. Or some alternative solution

Re: mutable keyword

2016-05-20 Thread ag0aep6g via Digitalmars-d-learn
On 05/20/2016 08:23 PM, Jack Applegame wrote: You can just cast const away: struct A { int id = 0; this(int id) { this.id = id; } void change() const { (cast() id)++; } } That's not a valid D program, though.

Re: mutable keyword

2016-05-20 Thread Jack Applegame via Digitalmars-d-learn
On Friday, 20 May 2016 at 17:28:55 UTC, Namespace wrote: But you can cheat: You can just cast const away: struct A { int id = 0; this(int id) { this.id = id; } void change() const { (cast() id)++; } }

Re: mutable keyword

2016-05-20 Thread Namespace via Digitalmars-d-learn
On Thursday, 19 May 2016 at 23:21:14 UTC, Jonathan M Davis wrote: On Thursday, May 19, 2016 20:44:54 ciechowoj via Digitalmars-d-learn wrote: Is there D equivalent of C++'s mutable keyword? Like the one that allows to modify a field of struct from constant method. Or some alternative solution

Re: mutable keyword

2016-05-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, May 19, 2016 20:44:54 ciechowoj via Digitalmars-d-learn wrote: > Is there D equivalent of C++'s mutable keyword? Like the one that > allows to modify a field of struct from constant method. Or some > alternative solution? No. D's const and immutable provide no backdoor

Re: mutable keyword

2016-05-19 Thread Era Scarecrow via Digitalmars-d-learn
On Thursday, 19 May 2016 at 20:44:54 UTC, ciechowoj wrote: Is there D equivalent of C++'s mutable keyword? Like the one that allows to modify a field of struct from constant method. Or some alternative solution? A little personal experience. I wrote a tool that had certain flags that did

Re: mutable keyword

2016-05-19 Thread QAston via Digitalmars-d-learn
On Thursday, 19 May 2016 at 20:44:54 UTC, ciechowoj wrote: Is there D equivalent of C++'s mutable keyword? Like the one that allows to modify a field of struct from constant method. Or some alternative solution? There isn't an equivalent of mutable keyword because D const differs from C

mutable keyword

2016-05-19 Thread ciechowoj via Digitalmars-d-learn
Is there D equivalent of C++'s mutable keyword? Like the one that allows to modify a field of struct from constant method. Or some alternative solution?