digitalmars.D.learn - "Semi-const" methods?
- Magnus Lie Hetland (18/18) Mar 13 2011 I have a data structure that's generally static (const, or even
- Magnus Lie Hetland (11/14) Mar 13 2011 OK -- a *little* quick on the trigger there. My solution: Declare the
- Magnus Lie Hetland (9/13) Mar 13 2011 Actually, I have a local (recursive) traversal function in the method I
- Jonathan M Davis (19/32) Mar 13 2011 What you want is logical const. You want it to be const from the perspec...
- Magnus Lie Hetland (5/16) Mar 14 2011 Thanks for the insights + tip :)
- Mafi (20/35) Mar 14 2011 I found away which doesn't use casts or bugs.
- Magnus Lie Hetland (5/7) Mar 14 2011 Nice :D
I have a data structure that's generally static (const, or even immutable), but it has some utility storage, which caches certain results during use. This caching etc. doesn't really affect the semantics of the main object, and are reset between operations, so I think it still would be useful to declare (and statically check) that certain methods don't modify any of the *rest* of the structure (i.e., the "main parts"). I *could* declare the methods const, and pass in a second object (a non-const parameter) for the caching etc. Or I cast the relevant parts to const (assigning them to local variables) early on in the relevant methods (dropping the const modifier on the method itself -- sort of a bummer). Any other ideas on how to handle this sort of "mostly const" or "const where it counts" stuff? Perhaps my design intentions here are off to begin with?-) -- Magnus Lie Hetland http://hetland.org
Mar 13 2011
On 2011-03-13 23:27:14 +0100, Magnus Lie Hetland said:Any other ideas on how to handle this sort of "mostly const" or "const where it counts" stuff? Perhaps my design intentions here are off to begin with?-)OK -- a *little* quick on the trigger there. My solution: Declare the method const, and assign the non-essential cache stuff to local variables, casting away the constness. (Still open to schooling on the design part of this, though. Perhaps declaring a method as const is no good when it's not *really* const? For now, I'm just doing it to check that I don't inadvertently change things I don't want to change.) -- Magnus Lie Hetland http://hetland.org
Mar 13 2011
On 2011-03-13 23:32:34 +0100, Magnus Lie Hetland said:(Still open to schooling on the design part of this, though. Perhaps declaring a method as const is no good when it's not *really* const? For now, I'm just doing it to check that I don't inadvertently change things I don't want to change.)Actually, I have a local (recursive) traversal function in the method I was talking about. Ended up not declaring the method as const, but declaring the argument of the traversal function as const. No misleading const declarations "outside", and I get the automatic checks that I want. -- Magnus Lie Hetland http://hetland.org
Mar 13 2011
On Sunday 13 March 2011 15:32:34 Magnus Lie Hetland wrote:On 2011-03-13 23:27:14 +0100, Magnus Lie Hetland said:What you want is logical const. You want it to be const from the perspective of an observer of the function but actually have stuff non-const within it. C++ has the mutable keyword to handle this. It's also completely legal and well-defined to cast away constness in C++. D, on the other hand, does not technically support logical const at all. It has to do with the complete lack of compiler guaranteeds. You _can_ cast away constness in D, but it's breaking the type system when you do. It is perfectly valid for the compiler to assume that you function really is const and optimize appropriately. So, if you don't actually manage to _really_ be logically const, or if you do this with an immutable object (which would likely result in a segfault), you _are_ going to have incorrect code. On the whole, I'd advise just not using const when you need logical const, but if you're _very_ careful, you can get away with it. But thanks to immutable, it can be _very_ dangerous to cast away constness in a const function unless you're _very_ careful. You really should check out this question on stackoverflow and the answers that go with it: http://stackoverflow.com/questions/4219600/logical-const-in-d - Jonathan M DavisAny other ideas on how to handle this sort of "mostly const" or "const where it counts" stuff? Perhaps my design intentions here are off to begin with?-)OK -- a *little* quick on the trigger there. My solution: Declare the method const, and assign the non-essential cache stuff to local variables, casting away the constness. (Still open to schooling on the design part of this, though. Perhaps declaring a method as const is no good when it's not *really* const? For now, I'm just doing it to check that I don't inadvertently change things I don't want to change.)
Mar 13 2011
On 2011-03-14 00:17:18 +0100, Jonathan M Davis said:So, if you don't actually manage to _really_ be logically const, or if you do this with an immutable object (which would likely result in a segfault), you _are_ going to have incorrect code. On the whole, I'd advise just not using const when you need logical const, but if you're _very_ careful, you can get away with it. But thanks to immutable, it can be _very_ dangerous to cast away constness in a const function unless you're _very_ careful. You really should check out this question on stackoverflow and the answers that go with it: http://stackoverflow.com/questions/4219600/logical-const-in-dThanks for the insights + tip :) -- Magnus Lie Hetland http://hetland.org
Mar 14 2011
Am 13.03.2011 23:27, schrieb Magnus Lie Hetland:I have a data structure that's generally static (const, or even immutable), but it has some utility storage, which caches certain results during use. This caching etc. doesn't really affect the semantics of the main object, and are reset between operations, so I think it still would be useful to declare (and statically check) that certain methods don't modify any of the *rest* of the structure (i.e., the "main parts"). I *could* declare the methods const, and pass in a second object (a non-const parameter) for the caching etc. Or I cast the relevant parts to const (assigning them to local variables) early on in the relevant methods (dropping the const modifier on the method itself -- sort of a bummer). Any other ideas on how to handle this sort of "mostly const" or "const where it counts" stuff? Perhaps my design intentions here are off to begin with?-)I found away which doesn't use casts or bugs. Just use delegates/closures. class C { int i; int delegate(int) getCache; this(int fi) { this.i = fi; int lastX, lastR; this.getCache = (int x) { if(x == lastX) return lastR; lastX = x; lastR = x * i; return lastR; }; } const multiply(int x) { return getCache(x); } }
Mar 14 2011
On 2011-03-14 11:51:09 +0100, Mafi said:I found away which doesn't use casts or bugs. Just use delegates/closures.Nice :D -- Magnus Lie Hetland http://hetland.org
Mar 14 2011