I just got it! (invariant/const)

Denton Cockburn diboss at hotmail.com
Wed Apr 9 06:55:23 PDT 2008


On Wed, 09 Apr 2008 13:50:59 +0100, Janice Caron wrote:

> On 09/04/2008, Jason House <jason.james.house at gmail.com> wrote:
>> The strange thing is that pure functions can't call invariant member functions of their invariant data.
> 
> I don't think that's correct.
> 
>     class C
>     {
>         int x;
> 
>         int f() invariant pure
>         {
>             return x;
>         }
>     }
> 
>     invariant c = cast(invariant) new C;
>     int n = c.f();
> 
> Should work just fine. Of course, the explicit cast necessary to
> create an invariant C in the first place is a bit ugly. Maybe we need
> "inew" to make new invariant objects?
> 

Couldn't the compiler insert the cast based on the declaration?

/* compiler inserts the cast since the declaration is
invariant */
invariant c = new C;

This would also make sense for const.  Is this possible? I think it would
be a nice nugget of syntactic sugar.




More information about the Digitalmars-d mailing list