www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compile-time reflection

Hey all,

I've been looking into creating a mocks library, and I've determined 
that most of it is possible. If someone gives me a delegate, I can mock 
it. (I think.) If someone gives me a class or interface, I can replace 
all its functions with null pointers so that using any unexpected 
function results in a segmentation fault.

I could replace all the methods of your class with a variadic method 
that just throws an exception. But variadic methods have the signature:
(TypeInfo[], void*, this)

That doesn't match arbitrary function signatures, just variadic function 

I could use a template function! No good. It wouldn't be instantiated at 
compile time because nobody calls it by name. I won't even be able to 
get a pointer to it to put in a vtbl.

I could mess with ClassInfo.vtbl at compile time! No good. It's a 
void*[] -- no way even to get the array length at compile time, much 
less muck about with pointers.

To do a proper job, I'd need the following, ideally:
struct function {
    void* addr;
    TypeInfo returnType;
    TypeInfo[] parameterTypes;
function func = &somefunc;
func.returnType.type x;

And, along with this, vtbls being filled by a template. (Templates 
affect the vtbl; templates need to read the vtbl; therefore, vtbl must 
be filled during template expansion and paying attention to dependencies.)

Without this, would anyone use a mocks library that segfaulted when you 
called a method without setting up expectations for it? Or one where you 
had to set up expectations, positive or negative, for every method of 
every mocked type?
Jul 15 2007