extensible classes and const
Yigal Chripun
yigal100 at gmail.com
Tue Apr 1 16:12:21 PDT 2008
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.
I've read an article which says a similar feature would be added to C#
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:
- B doesn't have to be a class, in fact the c# example used int to add
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...
More information about the Digitalmars-d
mailing list