Calling method by name.

Adam Ruppe destructionator at gmail.com
Fri Feb 4 08:17:09 PST 2011


Jacob Carlborg wrote:
> The class "Post" maps to the database table "posts", no configuration is
> necessary. Then you can use the column names in the table as fields to
> set and get data, like this:
>
> post = Post.new
> post.title = "some title"
> post.body = "the body"
> post.save # will update the database

Note that you can do this kind of thing with D's compile time
reflection and CTFE as well. That particular example works
in my own DataObject class (except I called it "commitChanges"
instead of "save").

Some of the stuff is just dynamic; just sugar around a string[string].
Most my own use of "runtime reflection" is just having some CTFE
go through and put the compile time info into those strings which
are used at runtime.

I do that for calling functions by name too, but mine does rely
upon some static info that probably isn't available to the OP.

For example for both, my new newsreader does both:

http://arsdnet.net/d-web-site/nntp/get-message

That screen is automatically generated by this prototype:

        Post getMessage(string newsgroup, string messageId) {

The name of the function and arguments in the URL are pulled from
that single line of code. It uses the types of the arguments to
automatically generate an appropriate form.


The way I did it was to put all the public functions in a static
struct. Then, I mixin a template (called FancyMain!(methodStruct))
which scans the struct and generates the needed hashmaps and
delegates to call its methods by name.

http://arsdnet.net/d-web-site/arsd/web.d

That code is very ugly... but look at the prepareReflection and
generateWrapper functions.


More information about the Digitalmars-d mailing list