www.digitalmars.com         C & C++   DMDScript  

D - Consider Generic/Template Modules

reply "Matthias Spycher" <matthias coware.com> writes:
For example:

module A (T, alias M : module) {
private import M;

class Boxed{
    T val;
...
}

Boxed f() {
    T x = M.g(); // we expect module M to provide g() for type T
    return Boxed(x);
}

}

---

module B;
private import A!(int, this);

static count = 0;
function int g() {
    return ++count;
}

Boxed!(int) y = f!();

---

One could basically inject the symbols of a module into another during
import and then resolve template members in the context of both scopes. In
other words, one can configure a module with the symbols from another.

I'm not sure how complicated this feature is to implement -- is it as bad as
general support for ADL. One could view it as a mechanism for selective ADL.
I believe they call it higher-order functors in some languages like SML.

Matthias
Jan 25 2004
parent reply "Walter" <walter digitalmars.com> writes:
You can do this now:

module A (T, alias M) {
class Boxed{
    T val;
...
}

Boxed f() {
    T x = M.g(); // we expect module M to provide g() for type T
    return Boxed(x);
}

}

---

module B;
private import A!(int, B);

static count = 0;
function int g() {
    return ++count;
}

Boxed!(int) y = f!();

---


"Matthias Spycher" <matthias coware.com> wrote in message
news:bv0po5$2ro4$1 digitaldaemon.com...
 For example:

 module A (T, alias M : module) {
 private import M;

 class Boxed{
     T val;
 ...
 }

 Boxed f() {
     T x = M.g(); // we expect module M to provide g() for type T
     return Boxed(x);
 }

 }

 ---

 module B;
 private import A!(int, this);

 static count = 0;
 function int g() {
     return ++count;
 }

 Boxed!(int) y = f!();

 ---

 One could basically inject the symbols of a module into another during
 import and then resolve template members in the context of both scopes. In
 other words, one can configure a module with the symbols from another.

 I'm not sure how complicated this feature is to implement -- is it as bad
as
 general support for ADL. One could view it as a mechanism for selective
ADL.
 I believe they call it higher-order functors in some languages like SML.

 Matthias
Jan 26 2004
parent "Matthias Spycher" <matthias coware.com> writes:
Very cool indeed!

I was thinking about a module configuration pattern that would allow us to
do AspectD using generic modules, but I have to give it some more thought...

Cheers
Matthias

"Walter" <walter digitalmars.com> wrote in message
news:bv4cev$2khv$1 digitaldaemon.com...
 You can do this now:

 module A (T, alias M) {
 class Boxed{
     T val;
 ...
 }

 Boxed f() {
     T x = M.g(); // we expect module M to provide g() for type T
     return Boxed(x);
 }

 }

 ---

 module B;
 private import A!(int, B);

 static count = 0;
 function int g() {
     return ++count;
 }

 Boxed!(int) y = f!();

 ---


 "Matthias Spycher" <matthias coware.com> wrote in message
 news:bv0po5$2ro4$1 digitaldaemon.com...
 For example:

 module A (T, alias M : module) {
 private import M;

 class Boxed{
     T val;
 ...
 }

 Boxed f() {
     T x = M.g(); // we expect module M to provide g() for type T
     return Boxed(x);
 }

 }

 ---

 module B;
 private import A!(int, this);

 static count = 0;
 function int g() {
     return ++count;
 }

 Boxed!(int) y = f!();

 ---

 One could basically inject the symbols of a module into another during
 import and then resolve template members in the context of both scopes.
In
 other words, one can configure a module with the symbols from another.

 I'm not sure how complicated this feature is to implement -- is it as
bad
 as
 general support for ADL. One could view it as a mechanism for selective
ADL.
 I believe they call it higher-order functors in some languages like SML.

 Matthias
Jan 26 2004