Unofficial wish list status.(Jul 2008)

Don nospam at nospam.com.au
Thu Jul 24 02:01:37 PDT 2008


Steven Schveighoffer wrote:
> "Jason House" wrote
>> Steven Schveighoffer Wrote:
>>
>>> "Jason House" wrote
>>>> Walter Bright Wrote:
>>>>> The missing ingredient here is what the 'pure' modifier for functions
>>>>> provides. That is the ability to diagnose attempts to alter global 
>>>>> state
>>>>> from within a function that is not supposed to do that. Invariant does
>>>>> not imply pure.
>>>>
>>>> That's actually a very key part to my whole point.  There is very 
>>>> little
>>>> difference between pure and invariant functions.  Why do we need a 
>>>> concept
>>>> for both?  Under the assumption that we're adding const to support
>>>> functional programming, I don't understand the motivation for this
>>>> difference.
>>> You are misunderstanding what an 'invariant' member function is.  The
>>> function itself is not invariant.  It's a function whose 'this' pointer 
>>> is
>>> marked as invariant.
>>
>> It's not that I misunderstand but that I question.  Given the goals of the 
>> const system, invariant functions, as defined, don't make a lot of sense. 
>> They make some sense, but not a lot of sense :)
> 
> They don't make any less sense than declaring invariant variables.  Or 
> declaring invariant parameters.
> 
> Can you see use in the following function signature:
> 
> void f(invariant(somestruct) *x) {...}
> 
> If so, an invariant member function is no different.

I think it is quite different. The member function is defined inside the 
class.

Think what it means for the 'this' parameter to be invariant. Unlike 
const, it's not conditional. An invariant instance can only call 
invariant and const member functions; it can never call mutable member 
functions. Likewise, a non-invariant instance can never call invariant 
member functions.
Seems to me, that you've actually defined 2 classes. There could, for 
example, be completely different vtables for invariant vs mutable instances.

interface ConstFoo
{
   .. constmemberfunctions
}

class MutableFoo: ConstFoo
{
}

class InvariantFoo: ConstFoo
{
}

Any instance of Foo is either an instance of MutableFoo, or of InvariantFoo.

I'm not sure why you wouldn't just make two classes, and apply invariant 
at the class definition level.



More information about the Digitalmars-d mailing list