www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Partial classes & forward declarations

reply Manu <turkeyman gmail.com> writes:
So I've been running into cases repeatedly all week where I really need one
or the other, or in some cases, both of these so solve some meta problems,
relating to rich bindings to a C++ code base.

I'm currently rolling with quite a few workarounds for various things, but
I've finally reached an impass.
Here's my problem, and maybe people can suggest some nice workaround to
this one...

I have a class, and it contains various method declarations. These methods
are to be implemented via mixins which generate stubs binding it to some
complex C++ call-through mechanism.
My approach was to allow the user to forward declare the methods, which
will tell the compiler all the information about the function call, and
then later on, a mixin would scan the class for such methods, and produce
the actual function stubs accordingly.
This doesn't work, because the compiler gives me a message that it is an
ambiguous call, even though it is actually the exact same function declared
twice (once as a forward declaration).

So apparently forward declarations aren't supported, even though they
appeared to be... then I found this: "The program is looked at as a whole,
and so not only is it not necessary to code forward declarations, it is not
even allowed! "
Well, in this case, a forward declaration is necessary, since it
effectively provides the compiler with the signature that it can use to
generate the function, in a way that looks exactly like a function
definition to anyone who is reading the code.
Having failed that, I have a workaround where I name the function with a
silly prefix, and make it private... but this makes no sense to someone
reading the source for the binding class (they see a weird list of private
mangled functions). So I'm not entirely happy with the solution, but it
does technically work for the moment.

There is a secondary problem with my workaround though... I can't figure
any way to *alias* a function argument, I can only get a typetuple of the
arguments. Since I don't have _alias_es of the function args, I have no way
to access their 'identifier'(/name), and that leads to a problem where I'm
generating the public stub function, and I have nothing to name the args
other than a0, a1, a2, etc...
That is complete rubbish, and unusable to an end user by my measure;
intellisense is useless if the pop-up argument list says a0,a1,a2. >_<

There's a third crudeness to my problem, and that is, to generate the
bindings, I need to put a mixin in every class. I already have a
'RegisterModule' mixin at the end of the file, which binds all the
'loose'/static methods.
What would be really handy in this case, is to be able to use partial

classes all within the one RegisterModule at the end.
I think partial classes will become considerably more useful too when we
have attributes, and some may classes need to have some extra data members
added to support their given attributes.

So let's ignore partial classes for the moment, I'm happy enough with the
meta spam for the time being. What kinds of tricks can I use to generate
the stub functions I want, while preserving the argument names, and
sensible declaration/readibility of the module. How else could the
functions be declared, short of a string literal, is there any way I can
pull out the names of args currently?

I think there's definitely room for a trait to refer to the _instance_ of a
function arg in the same way as 'getMember', thus allowing it to be
enumerated+supplied to templates as alias parameters.
Apr 20 2012
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-04-20 14:07, Manu wrote:

 There is a secondary problem with my workaround though... I can't figure
 any way to *alias* a function argument, I can only get a typetuple of
 the arguments. Since I don't have _alias_es of the function args, I have
 no way to access their 'identifier'(/name), and that leads to a problem
 where I'm generating the public stub function, and I have nothing to
 name the args other than a0, a1, a2, etc...
 That is complete rubbish, and unusable to an end user by my measure;
 intellisense is useless if the pop-up argument list says a0,a1,a2. >_<
Don't know if it helps but you can get the name of the arguments of a function: https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L29 I don't think that function handles delegate/function parameters. -- /Jacob Carlborg
Apr 20 2012
next sibling parent reply Manu <turkeyman gmail.com> writes:
On 20 April 2012 17:19, Jacob Carlborg <doob me.com> wrote:

 On 2012-04-20 14:07, Manu wrote:

  There is a secondary problem with my workaround though... I can't figure
 any way to *alias* a function argument, I can only get a typetuple of
 the arguments. Since I don't have _alias_es of the function args, I have
 no way to access their 'identifier'(/name), and that leads to a problem
 where I'm generating the public stub function, and I have nothing to
 name the args other than a0, a1, a2, etc...
 That is complete rubbish, and unusable to an end user by my measure;
 intellisense is useless if the pop-up argument list says a0,a1,a2. >_<
Don't know if it helps but you can get the name of the arguments of a function: https://github.com/jacob-**carlborg/orange/blob/master/** orange/util/Reflection.d#L29<https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L29>
Amazing, that needs to go in std.traits pronto! :P This should help with my hack in the mean time. I don't think that function handles delegate/function parameters. Bummer. I still think there's room for a formal trait to do this. Complimentary to allMembers and getMember, but for a functions.
Apr 20 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-04-20 16:47, Manu wrote:

 Bummer. I still think there's room for a formal trait to do this.
 Complimentary to allMembers and getMember, but for a functions.
The function currently just splits the string on ",". It would be possible to add a more sophisticated algorithm to handle delegate and function pointer parameters. -- /Jacob Carlborg
Apr 21 2012
parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 21-04-2012 16:32, Jacob Carlborg wrote:
 On 2012-04-20 16:47, Manu wrote:

 Bummer. I still think there's room for a formal trait to do this.
 Complimentary to allMembers and getMember, but for a functions.
The function currently just splits the string on ",". It would be possible to add a more sophisticated algorithm to handle delegate and function pointer parameters.
It would basically mean having to parse D's full type syntax to be 100% correct, which is not cool. The compiler ought to help out here. -- - Alex
Apr 21 2012
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-04-21 17:45, Alex Rønne Petersen wrote:

 It would basically mean having to parse D's full type syntax to be 100%
 correct, which is not cool. The compiler ought to help out here.
Yet again we need a compiler library. -- /Jacob Carlborg
Apr 21 2012
prev sibling parent Manu <turkeyman gmail.com> writes:
On 21 April 2012 18:45, Alex R=C3=B8nne Petersen <xtzgzorex gmail.com> wrot=
e:

 On 21-04-2012 16:32, Jacob Carlborg wrote:

 On 2012-04-20 16:47, Manu wrote:

  Bummer. I still think there's room for a formal trait to do this.
 Complimentary to allMembers and getMember, but for a functions.
The function currently just splits the string on ",". It would be possible to add a more sophisticated algorithm to handle delegate and function pointer parameters.
It would basically mean having to parse D's full type syntax to be 100% correct, which is not cool. The compiler ought to help out here.
Some traits would be nice; allArguments, getArgument. This would allow enumeration of argument lists in the exact same way as struct members. D has gone hard out with its compile time meta capabilites, but there's just a couple of small things missing from the complete introspection set.
Apr 21 2012
prev sibling parent reply "Jakob Ovrum" <jakobovrum gmail.com> writes:
On Friday, 20 April 2012 at 14:19:25 UTC, Jacob Carlborg wrote:
 Don't know if it helps but you can get the name of the 
 arguments of a function:

 https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L29

 I don't think that function handles delegate/function 
 parameters.
This function is too naive, it's incredibly easy to break. void foo(immutable(int) a);
Apr 20 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-04-20 18:21, Jakob Ovrum wrote:
 On Friday, 20 April 2012 at 14:19:25 UTC, Jacob Carlborg wrote:
 Don't know if it helps but you can get the name of the arguments of a
 function:

 https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L29


 I don't think that function handles delegate/function parameters.
This function is too naive, it's incredibly easy to break. void foo(immutable(int) a);
Yeah, I know. This function more or less shows it's possible. -- /Jacob Carlborg
Apr 21 2012
prev sibling parent "Martin Nowak" <dawg dawgfoto.de> writes:
 I have a class, and it contains various method declarations. These  
 methods
 are to be implemented via mixins which generate stubs binding it to some
 complex C++ call-through mechanism.
 My approach was to allow the user to forward declare the methods, which
 will tell the compiler all the information about the function call, and
 then later on, a mixin would scan the class for such methods, and produce
 the actual function stubs accordingly.
Not sure if I understood you problem correctly, but these are the two idioms I use regularly. //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: module lib; mixin template genFuncAlias(string fname, RT, Args...) { RT __genFunc(Args args) { //... return RT.init; } mixin("alias __genFunc "~fname~";"); } mixin template genFuncName(string fname, RT, Args...) { enum __s = codegen(fname); mixin(__s); } string codegen(string fname) { import std.array, std.format; auto app = appender!string(); formattedWrite(app, q{ RT %1$s(Args args) { //... return RT.init; } }, fname); return app.data; } //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: module client; import lib; class Blah { // not overloadable ?? mixin genFuncAlias!("bar", int, string, double); // mixin genFuncAlias!("bar", int, double, string); mixin genFuncName!("foo", int, string, double); mixin genFuncName!("foo", int, double, string); } void main() { auto b = new Blah(); b.bar("1.0", 1.0) && assert(0); // not overloadable ?? // b.bar(2.0, "2.0") && assert(0); b.foo("1.0", 1.0) && assert(0); b.foo(2.0, "2.0") && assert(0); } //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: dmd -run client lib
Apr 21 2012