www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - behavior of this

reply Ender KaShae <astrothayne gmail.com> writes:
I think that it would be nice to define how this behave for example:

static func(ref this);

would be called as a.func() which would be equivalent to class.func(a); i.e.
this would be a reference to the reference that is calling the function.

or 

static func(invarient this);  would declare that the members of this are
constant and can not be changed, if this was implemented then invarient
instances of the class should only be able to call methods that have invarient
this as a parameter

i'm not sure if this will work but it would be very useful, especially for
immutable types.
Jul 25 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Ender KaShae" <astrothayne gmail.com> wrote in message 
news:f88h4u$31g2$1 digitalmars.com...
 static func(invarient this);  would declare that the members of this are 
 constant and can not be changed, if this was implemented then invarient 
 instances of the class should only be able to call methods that have 
 invarient this as a parameter

Const and invariant methods already exist, though not in the odd "static with this as first parameter" way that you're proposing. class A { const int foo() { // Here I can only call other const/invariant methods // and can't modify the state of the object } invariant int bar() { // Similar but even stricter; can only call invariant // methods and access invariant fields } }
Jul 25 2007