openmethods

This module implements fast open multi-_methods.

Open _methods are like virtual functions, except that they are free functions, living outside of any class. Multi-_methods can take into account the dynamic types of more than one argument to select the most specialized variant of the function.

This implementation uses compressed dispatch tables to deliver a performance similar to ordinary virtual function calls, while minimizing the size of the dispatch tables in the presence of multiple virtual arguments.

Synopsis of openmethods:

1 
2 import openmethods; // import lib
3 mixin(registerMethods); // mixin must be called after importing module
4 
5 interface  Animal {}
6 class Dog : Animal {}
7 class Pitbull : Dog {}
8 class Cat : Animal {}
9 class Dolphin : Animal {}
10 
11 // open method with single argument <=> virtual function "from outside"
12 string kick(virtual!Animal);
13 
14 @method // implement 'kick' for dogs
15 string _kick(Dog x) // note the underscore
16 {
17   return "bark";
18 }
19 
20 @method("kick") // use a different name for specialization
21 string notGoodIdea(Pitbull x)
22 {
23   return next!kick(x) ~ " and bite"; // aka call 'super'
24 }
25 
26 // multi-method
27 string meet(virtual!Animal, virtual!Animal);
28 
29 // 'meet' implementations
30 @method
31 string _meet(Animal, Animal)
32 {
33   return "ignore";
34 }
35 
36 @method
37 string _meet(Dog, Dog)
38 {
39   return "wag tail";
40 }
41 
42 @method
43 string _meet(Dog, Cat)
44 {
45   return "chase";
46 }
47 
48 void main()
49 {
50   updateMethods(); // once per process - don't forget!
51 
52   import std.stdio;
53 
54   Animal hector = new Pitbull, snoopy = new Dog;
55   writeln("kick snoopy: ", kick(snoopy)); // bark
56   writeln("kick hector: ", kick(hector)); // bark and bite
57 
58   Animal felix = new Cat, flipper = new Dolphin;
59   writeln("hector meets felix: ", meet(hector, felix)); // chase
60   writeln("hector meets snoopy: ", meet(hector, snoopy)); // wag tail
61   writeln("hector meets flipper: ", meet(hector, flipper)); // ignore
62 }

Members

Classes

MethodError
class MethodError

Information passed to the error handler function.

Functions

next
auto next(T args)

Call the next most specialized override, if it exists. In other words, call the override that would have been called if this one had not been defined.

registerMethods
auto registerMethods(string moduleName = __MODULE__)

Used as a string mixin: register the method declarations and definitions in the current module.

setMethodErrorHandler
void function(MethodError error) setMethodErrorHandler(void function(MethodError error) handler)

Set the error handling function to be called if an open method cannot be called with the provided arguments. The default is to abort the program.

updateMethods
Runtime.Metrics updateMethods()

Update the runtime dispatch tables. Must be called once before calling any methods. Typically this is done at the beginning of main.

Structs

covariant
struct covariant(T)

Mark a parameter as covariant.

method
struct method

Attribute: Add an override to a method.

mptr
struct mptr

Attribute: Set the policy for storing and retrieving the method pointer (mptr).

virtual
struct virtual(T)

Mark a parameter as virtual, and declare a method.

Meta

Authors

Jean-Louis Leroy 2017