www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - extensible classes and const

one of the feature suggested for D2 is extensible classes, where the
programmer can write functions outside the class that their first
argument is the class:
---
class C { ...}
void func (C a, int b) { ...}

auto c = new C;
// t
func(c, 3);
c.func(3);
---
the above two calls for func are equivalent.

and they use a syntax such as:
A func(this B b, C c); // assume A, B, C are types.
this adds the function func to type B and allows you to use b.func(c) .
two remarks about the above syntax:

a method to int.
 - I've just realized that it could be used to specify the constancy of
this.
 
regarding the second point: we can mix that with D's type inference and
provide shortcuts:
what about:
void func(this, int a); // in a method type of "this" is known.
void func(const(this), int a);
or maybe:
void func(auto this, int a); // type is inffered
void func(const this, int a);

the current version of:
void func(int a);
would become a shortcut for the "full" version of the appropriate code
above.

-- Yigal

PS: everywhere i wrote const, you can use invariant as well...
Apr 01 2008