www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opDispatch or equivalent at static context

reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
(Apologies ahead of time if I've overlooked something.)

How possible could it be to have opDispatch or an equivalent feature
(opStaticDispatch?) 
available for static forwarding?  Use-case: I'm envisioning an ORM library,
where one 
could do things like:
	Player.findFirstByName( "Bob" )

With a static forwarding template parsing the "findFirstByName" to rewrite the
call:
	Player.find( FindFlags.First, `name == "Bob"` )

I can't help but think there could be other useful scenarios.  The current
(untested) 
work-around would be a proxy object for the API:
	auto finder = new Finder!Player;
	finder.findFirstByName( "Bob" );

	// or...
	auto finder = new Finder;
	finder.findFirstPlayerByName( "Bob" );

The obvious problem with an opStaticDispatch, of course, would be preserving
propagation 
of static lookups along the inheritance chain.  I'm guessing this could be
surmountable 
when using conditions though.

	class Foo : Base {
		static Foo opStaticDispatch ( string s, T t ) ( t param )
		if ( s.length > 3 && s[ 0 .. 4 ] == "find" ) {
			// do stuff
			// if the condition failed, check for Base.opStaticDispatch's
		}
	}

If there were a means for static member functions to be aware that they are
being called 
through a sub-class, that might also be an avenue to the abilities I'm wanting.
 I can't 
think of any sane means for that, however.

-- Chris Nicholson-Sauls
Jan 20 2010
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Chris Nicholson-Sauls wrote:
 (Apologies ahead of time if I've overlooked something.)
 
 How possible could it be to have opDispatch or an equivalent feature 
 (opStaticDispatch?) available for static forwarding?  Use-case: I'm 
 envisioning an ORM library, where one could do things like:
     Player.findFirstByName( "Bob" )
 
 With a static forwarding template parsing the "findFirstByName" to 
 rewrite the call:
     Player.find( FindFlags.First, `name == "Bob"` )
Isn't that what opDispatch does now? -Lars
Jan 21 2010
parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Lars T. Kyllingstad wrote:
 Chris Nicholson-Sauls wrote:
 (Apologies ahead of time if I've overlooked something.)

 How possible could it be to have opDispatch or an equivalent feature 
 (opStaticDispatch?) available for static forwarding?  Use-case: I'm 
 envisioning an ORM library, where one could do things like:
     Player.findFirstByName( "Bob" )

 With a static forwarding template parsing the "findFirstByName" to 
 rewrite the call:
     Player.find( FindFlags.First, `name == "Bob"` )
Isn't that what opDispatch does now? -Lars
Not as a static member, to my knowledge. At the very least, I see no mention of static forwarding in the docs: http://www.digitalmars.com/d/2.0/operatoroverloading.html#Dispatch -- Chris Nicholson-Sauls
Jan 21 2010
next sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
On 01/21/2010 09:28 AM, Chris Nicholson-Sauls wrote:
 Lars T. Kyllingstad wrote:
 Chris Nicholson-Sauls wrote:
 (Apologies ahead of time if I've overlooked something.)

 How possible could it be to have opDispatch or an equivalent feature
 (opStaticDispatch?) available for static forwarding? Use-case: I'm
 envisioning an ORM library, where one could do things like:
 Player.findFirstByName( "Bob" )

 With a static forwarding template parsing the "findFirstByName" to
 rewrite the call:
 Player.find( FindFlags.First, `name == "Bob"` )
Isn't that what opDispatch does now? -Lars
Not as a static member, to my knowledge. At the very least, I see no mention of static forwarding in the docs: http://www.digitalmars.com/d/2.0/operatoroverloading.html#Dispatch -- Chris Nicholson-Sauls
static opDispatch works fine.
Jan 21 2010
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Chris Nicholson-Sauls wrote:

 Isn't that what opDispatch does now?

 -Lars
Not as a static member, to my knowledge. At the very least, I see no mention of static forwarding in the docs: http://www.digitalmars.com/d/2.0/operatoroverloading.html#Dispatch
The fact that the name of the non-existent function arrives as a template parameter indicates that it is static dispatch. Which made me realize that it can be overloaded: void opDispatch(string name : "special_member_function_name", T)(T parameter) { // special implementation for "special_member_function_name" } Ali
Jan 21 2010