unpaintable (the solution to logical const)
Simen Kjaeraas
simen.kjaras at gmail.com
Fri Apr 4 11:34:07 PDT 2008
On Fri, 04 Apr 2008 20:09:43 +0200, Janice Caron <caron800 at googlemail.com>
wrote:
> On 04/04/2008, Simen Kjaeraas <simen.kjaras at gmail.com> wrote:
>> What reason would exist for having an unpaintable invariant field? As
>> far
>> as I've understood, invariant is implicitly castable to const
>
> So is mutable.
>
> I don't know what the uses would be. C++ doesn't have it. No language
> that I'm aware of has it. This is new. Like mixins - when they were
> first introduced, Walter said words to the effect of "I've no idea
> what these things will be used for, but they might be useful, so let's
> wait and see".
>
> No one is suggesting there is a specific need to create permanently
> invariant fields, but people /are/ suggesting we need logical const,
> and this is a solution (...and so far as we are aware, the /only/
> solution...) that works. That it gives us unpaintable invariant fields
> as a side-effect is an interesting bonus.
>
>> and casting
>> away const/invariant is a Bad Thing,
>
> Right - which is why this scheme specifically avoids any need to do
> that. Everything is tightly constrained.
I agree on all points. At first I felt this whole idea was foolish, but as
I read more and started groking it, I see it has its uses, and the
solution seems elegant.
>> so whether it is const or invariant
>> does little difference, except for functions that demand invariant
>> arguments.
>
> And there may well be those - especially when it comes to pure functions.
It would appear I messed up some text in my post. I was thinking of
/paintable/ invariant fields. Unpaintable ones are ok. Paintable
invariants I'm not so sure of. Take the following example:
class foo
{
invariant int bar;
}
int somePureFunction(invariant(foo) f)
{
return f.bar;
}
void main()
{
foo f = new foo();
auto a = somePureFunction(f);
const foo g = new foo();
auto b = somePureFunction(g); // error here, due to g.bar being const,
not invariant
}
Now to find a better name than paintable, and convincing Walter...
-- Simen
More information about the Digitalmars-d
mailing list