Summary: Extension methods
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from Witold Baryluk <> 2010-02-05 
06:20:17 PST ---
Long time ago there was a idea to have extending (well this isn't best name) of
the class implementations.

This is my proposition:

module m1;
class A {
   int x() { retutn 5 }

module m2;
import m1;
float y(A a) { return a.x() * 11.1; }

float z(A a) { return a.y() + 22.1; }

module test;
import m1, m2;
A a = new A();
assert(a.y() == 55.5);
assert(a.z == 77.6);

Questoin remains if y(a) is valid function call.
This is not necasarly best way becuase it would be best if y, z method will
have access to private fields of A. It also should be possible tu use this in
them (this can be emulated using with:

float y(A this) { with(this) { return x() * 11.1; } }

Actually this is similar to my suggested problem with array (and not only)
I.e. a.find(b) is transleted to find(a, b).

It should be explicitly be written:
int find(string a, string b) ....

Othere asspect is interference with the template functions. One can't easly
have things like:

int something(T)(T a) { return 42; }

becuase this method can extend all types. And this is problematic for two
 - conflicts of such widely applicable functions.
 - efficiency of searching for such functions. (but it shouldn't be actually so
big problem if this information is cached for each type on first usage).

This also makes questionably if it should have such syntax, more appropriate
will be nonredudant:

int something(T)(T a)  { return 42; }

Other possiblity (AFAIK used in C#, but don't remember exactly, is to use

int something(this A a) { } 

int something(this A this) { }

Where first this is indicating that this is extending function.
Second this is just convinient name for usage inside of something(A) function
(preferably with with, evetntually with(this) should be automatically put

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to