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

Aliases

MethodErrorHandler
alias MethodErrorHandler = void function(MethodError error)
Undocumented in source.

Classes

MethodError
class MethodError

Information passed to the error handler function.

Enums

IsCovariant
eponymoustemplate IsCovariant(T)
Undocumented in source.
IsCovariant
eponymoustemplate IsCovariant(T : covariant!U, U)
Undocumented in source.
IsVirtual
eponymoustemplate IsVirtual(T)
Undocumented in source.
IsVirtual
eponymoustemplate IsVirtual(T : virtual!U, U)
Undocumented in source.

Functions

_declareMethod
string _declareMethod()
Undocumented in source. Be warned that the author may not have intended to support it.
_registerMethods
string _registerMethods()
Undocumented in source. Be warned that the author may not have intended to support it.
defaultMethodErrorHandler
void defaultMethodErrorHandler(MethodError error)
Undocumented in source. Be warned that the author may not have intended to support it.
needUpdateMethods
bool needUpdateMethods()
Undocumented in source. Be warned that the author may not have intended to support it.
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)

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.

Mixin templates

_registerSpecs
mixintemplate _registerSpecs(alias MODULE)
Undocumented in source.
declareMethod
mixintemplate declareMethod(string index, ReturnType, string name, ParameterType...)
Undocumented in source.
declareMethod
mixintemplate declareMethod(ReturnType, string name, ParameterType...)
Undocumented in source.
defineMethod
mixintemplate defineMethod(alias Dispatcher, alias Fun)
Undocumented in source.
registerClasses
mixintemplate registerClasses(Classes...)
Undocumented in source.

Structs

Method
struct Method(string Mptr, R, string id, T...)
Undocumented in source.
MethodTag
struct MethodTag
Undocumented in source.
Runtime
struct Runtime
Undocumented in source.
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.

Templates

castArgs
template castArgs(T...)
Undocumented in source.
getUDAs
template getUDAs(alias symbol, alias attribute)
Undocumented in source.

Variables

MptrInDeallocator
auto MptrInDeallocator;
Undocumented in source.
MptrViaHash
auto MptrViaHash;
Undocumented in source.
errorHandler
MethodErrorHandler errorHandler;
Undocumented in source.
hasVirtualParameters
bool hasVirtualParameters(alias F);
Undocumented in source.

Meta

Authors

Jean-Louis Leroy 2017