digitalmars.D - Logic const with lazy const
- bearophile (29/29) Jul 06 2014 I guess other persons have already suggested something like this.
- Walter Bright (5/8) Jul 06 2014 Not specifically. My opinion on logical const isn't consensus, but I bel...
- CarolFargo (5/5) Jul 07 2014 okay, that is a great information for me. I have read some kind
I guess other persons have already suggested something like this.
Here there is a "lazy immutable" instance member x, that can be
only in two states: not initialized, and initialized with an
immutable value.
The type system ensures x is never read before it's written, and
that it's written no more than once (and to do this I presume it
needs flow analysis).
The point of this idea is to make the bar() method const despite
the x instance attribute is computed only if it's needed. This is
useful is spam() requires significant computations, but x is not
always needed. So it allows bar() to be "logically const" in a
verified correct way.
int spam() pure nothrow safe nogc {
// Lot of computations here.
return 0;
}
struct Foo {
private lazy immutable int x;
static assert (this.x.sizeof == 8);
int bar() const pure nothrow safe nogc {
if (this.x.isNull) {
this.x = spam();
}
return this.x;
}
}
void main() {}
Bye,
bearophile
Jul 06 2014
On 7/6/2014 7:20 AM, bearophile wrote:I guess other persons have already suggested something like this. Here there is a "lazy immutable" instance member x, that can be only in two states: not initialized, and initialized with an immutable value.Not specifically. My opinion on logical const isn't consensus, but I believe that logical constness is way, way down on the list of things that a type system can productively support. It's easily handled with existing features by using a struct.
Jul 06 2014
okay, that is a great information for me. I have read some kind of related information from a site (<a href="http://www.buyessays.us/phd-thesis-writing.php">weblink </a> ). There some what additional information is there. Thank you
Jul 07 2014









Walter Bright <newshound2 digitalmars.com> 