opDispatch or equivalent at static context

Chris Nicholson-Sauls ibisbasenji at gmail.com
Wed Jan 20 23:59:30 PST 2010


(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



More information about the Digitalmars-d mailing list