|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
digitalmars.D.learn - Static opCall Factory Method Doesn't Work for Inner Classes
Hello fellow D'ers...
I'm writing a message passing kernel and scheduler. "User-space" programs
communicate with each other and the kernel through inboxes and outboxes which
are allocated and tracked by the kernel. If a user-space program wants to
allocate an inbox called "stdin" and an outbox called "stdout" currently they
simply ask the kernel to create one for them.
For example:
auto my_stdin = kernel.createInbox(String)("stdin");
auto my_stdout = kernel.createOutbox(String)("stdout");
This works fine for me. It is a pain, however, to make a new "create" method
everytime I want to define a new structure.
I'd much rather use the "static <classname> opCall" mechanism which allows you
to simply call the classname to have it generate an instance of that class.
Now the static opCall pattern I've gotten to work:
class A {
...
static A opCall(args) {
return new A(args);
}
}
auto a = A(args);
That works fine if you want instances of a factory created by calling the
factory. For example:
CarFactory car = CarFactory(args);
It's a little syntactically awkward, but it works. My problem is I want the
kernel to act as a factory for inboxes, outboxes and a bunch of other things.
I'd like this to work:
class Kernel {
...
class Inbox {
...
static Inbox opCall(char[] name) {
return new Inbox(name);
}
}
class Outbox {
...
static Outbox opCall(char[] name) {
return new Outbox(name);
}
}
...
}
That way I just create a static opCall method for each kernel object and I get
a factory method which could be used like this...
auto my_inbox = kernel.Inbox("stdin");
auto my_outbox = kernel.Outbox("stdout");
BUT....
Every syntax combination in D to express a static opcall on an inner class
doesn't work. Both 'ldc' and 'gdc' generate compiler errors that say that
'this' is not available for static opCalls. When I take the 'static' keyword
off the opCall declaration the compiler error becomes something like: 'this' is
required to access the opCall method.
I was getting pretty tired when I gave up on this last night, but the problem
is still eluding me. Am I just trying to do this the wrong way? I know the
"createInbox" method works. Perhaps I should make separate factories for every
structure, but that seems harder than just making create methods.
If I can get this to work, I think I could create a mixin that would generate
automatic factory methods.
(This is probably obvious and I was just too tired last night.)
Thanks for your help,
eris
Jan 12 2010
eris <jvburnes gmail.com> wrote:Every syntax combination in D to express a static opcall on an inner class doesn't work. Both 'ldc' and 'gdc' generate compiler errors that say that 'this' is not available for static opCalls. When I take the 'static' keyword off the opCall declaration the compiler error becomes something like: 'this' is required to access the opCall method. Jan 12 2010
Simen kjaeraas Wrote:eris <jvburnes gmail.com> wrote:Every syntax combination in D to express a static opcall on an inner class doesn't work. Both 'ldc' and 'gdc' generate compiler errors that say that 'this' is not available for static opCalls. When I take the 'static' keyword off the opCall declaration the compiler error becomes something like: 'this' is required to access the opCall method. Jan 12 2010
|