rebind of const class variables
I am writing a tree data structure, and I have the following code: ```D final class Node { private { int val_; Node parent_; Node left_; Node right_; } @property const(Node) maximum() const { auto ret = this; while (ret.right_) { ret = ret.right_; } return ret; } } ``` It failed to compile and complaint that `cannot modify const expression ret`。 Since `ret` is just a binding to a const class object, why can't I rebind it to another const class variable? Must I use pointers to cope with this? Thx
Re: rebind of const class variables
On Tuesday, 20 January 2015 at 09:29:46 UTC, qqiang wrote: I am writing a tree data structure, and I have the following code: ```D final class Node { private { int val_; Node parent_; Node left_; Node right_; } @property const(Node) maximum() const { auto ret = this; while (ret.right_) { ret = ret.right_; } return ret; } } ``` It failed to compile and complaint that `cannot modify const expression ret`。 Since `ret` is just a binding to a const class object, why can't I rebind it to another const class variable? Must I use pointers to cope with this? Thx You are looking for http://dlang.org/phobos/std_typecons.html#.Rebindable
Re: rebind of const class variables
On Tuesday, January 20, 2015 09:29:45 qqiang via Digitalmars-d-learn wrote: I am writing a tree data structure, and I have the following code: ```D final class Node { private { int val_; Node parent_; Node left_; Node right_; } @property const(Node) maximum() const { auto ret = this; while (ret.right_) { ret = ret.right_; } return ret; } } ``` It failed to compile and complaint that `cannot modify const expression ret`。 Since `ret` is just a binding to a const class object, why can't I rebind it to another const class variable? Must I use pointers to cope with this? No. You need to use http://dlang.org/phobos/std_typecons.html#.Rebindable The thing to remember is that const is transitive, and when you have const MyClass foo; the reference itself is const, not just what it refers to. And the type system doesn't have a way to represent a mutable reference to a const class object, because it has no way of representing classes separately from references to them. The two are, for better or worse, very much conflated as far as the language is concerned. Pointers don't have that problem. e.g. const(MyStruct)* foo; but if you did const(MyClass)* foo; you'd just end up with a pointer to a const reference to a class object and not a pointer to a const class object. Rebindable works around the problem by being a mutable holder for the const reference to the class. It's a bit a annoying and clunky, but it's a side effect of how there really isn't any way in the language to represent class objects separately from the references to them. - Jonathan M Davis
Re: rebind of const class variables
On Tue, 20 Jan 2015 09:29:45 + qqiang via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: I am writing a tree data structure, and I have the following code: ```D final class Node { private { int val_; Node parent_; Node left_; Node right_; } @property const(Node) maximum() const { auto ret = this; while (ret.right_) { ret = ret.right_; } return ret; } } ``` It failed to compile and complaint that `cannot modify const expression ret`。 Since `ret` is just a binding to a const class object, why can't I rebind it to another const class variable? Must I use pointers to cope with this? Jonathan explains it very well. i can add the only thing: don't use `const` until you forced to. ;-) C++ programmers tend to help compiler with const methods and so on. just don't do that in D until you become friends with D constness. sure, you can cast `const` away in your code, but using `cast` is a good sign of taking the wrong way. signature.asc Description: PGP signature
Re: rebind of const class variables
ketmar: Jonathan explains it very well. i can add the only thing: don't use `const` until you forced to. ;-) In D use immutable (or const) everywhere you can. Possibly mark as immutable everything doesn't need to mutate. sure, you can cast `const` away in your code, but using `cast` is a good sign of taking the wrong way. Casting away a const is quite dangerous in D because if you later mutate the data, you will probably have bugs. Bye, bearophile
Re: rebind of const class variables
On Tue, 20 Jan 2015 14:45:26 + bearophile via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: ketmar: Jonathan explains it very well. i can add the only thing: don't use `const` until you forced to. ;-) In D use immutable (or const) everywhere you can. Possibly mark as immutable everything doesn't need to mutate. i didn't tell a word about `immutable`. ;-) but `const` transitiveness confusing newcomers alot. eventually they will catch it and then they will see how my advice is not applicable to 'em anymore. but until then i believe that they better don't use `const` at all instead of becoming constantly frustrated with it's non-friendly behavior. signature.asc Description: PGP signature