Unofficial wish list status.(Jul 2008)

Koroskin Denis 2korden at gmail.com
Wed Jul 23 11:11:33 PDT 2008


On Wed, 23 Jul 2008 22:01:34 +0400, Simen Kjaeraas  
<simen.kjaras at gmail.com> wrote:

> Steven Schveighoffer <schveiguy at yahoo.com> wrote:
>
>> In fact, a pure member function would have tobe marked as:
>>  pure invariant void f();
>
> While I agree a pure member function would also need to be invariant,
> it seems superfluous to have to write that in the function signature.
> It /has/ to be, so why should I need to specify it?
>
>> As for how useful would an invariant (but not pure) function be?   
>> Consider
>> something like:
>>
>> class C
>> {
>>    int x;
>>    invariant void printTo(stream s) { s.print(x); }
>> }
>
> Could this not just as easily have been done with a const
> function, as invariant is implicitly castable to const?
>
> -- Simen


There might be a benefit, as it enforces stronger guaranties.
Suppose that our class is large storage container and stream.print is a  
time-consuming process that may take signaficant amount of time. If an  
object is invariant then then there is a guarantee than its data won't be  
changed in another thread during function execution (at the very least).  
And given that, there is no need to synchronize data, otherwise it should  
be rewritten as synchronized:

class C
{
    Data data;

    synchronized const void printTo(stream s) { s.print(data); }
    invariant void printTo(stream s) { s.print(data); }              //  
invariance
}

On Wed, 23 Jul 2008 04:35:16 +0400, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> 5. Invariant data does not have to be synchronized to be multithread  
> accessible.



More information about the Digitalmars-d mailing list