Let's not make invariants const
Era Scarecrow
rtcvb32 at yahoo.com
Sat Aug 4 00:06:19 PDT 2012
On Saturday, 4 August 2012 at 06:53:16 UTC, Mehrdad wrote:
> It's worth pointing out the obvious, I.e. that as long as
> 'const' is physical const instead of logical const (which is
> always), invariants and contracts can't be const, because that
> would restrict them from calling methods that are logically but
> not physically const.
>
> So I think they probably shouldn't be const.
As mentioned before, doesn't 'const' _only_ apply to the current
(this) object and not anything else? Besides the contracts can't
have lasting side effects that would change logical execution.
How would changing the current object in an invariant be correct
in release code?
I'm not sure if the contracts should have many (or any)
restrictions, but at the same time if you remove the contracts,
nothing in the code should change the execution; Example: using
writeln in an invariant should not change the current
object/structure in any form, or anything it is part of. Although
not writeln is not 'pure', const shouldn't prevent you from using
it (pure or @safe declarations may withhold you).
More information about the Digitalmars-d
mailing list