Task

Demonstrate how to make the object respond (sensibly/usefully) to an invocation of a method on it that it does not support through its class definitions.

Note that this is not the same as just invoking a defined method whose name is given dynamically; the method named at the point of invocation must not be defined.

This task is intended only for object systems that use a dynamic dispatch mechanism without static checking.



Related task

   Send an unknown method call.


with javascript_semantics
enum METHODS
 
function invoke(object o, string name, sequence args={})
--(this works on any class, for any function, with any number or type of parameters)
    integer mdict = o[METHODS]
    integer node = getd_index(name,mdict)
    if node!=0 then
        return call_func(getd_by_index(node,mdict),args)
    end if
    return "no such method" -- or throw(), fatal(), etc
end function
 
--class X: Xmethods emulates a vtable
constant Xmethods = new_dict()
 
function exists()
    return "exists"
end function
 
setd("exists",routine_id("exists"),Xmethods)
 
--class X: create new instances
function newX()
    return {Xmethods}
end function
 
object x = newX()
 
?invoke(x,"exists")
?invoke(x,"non_existent_method")
