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 presence of multiple virtual arguments.

Synopsis of openmethods:

1 
2 import openmethods; // import lib
3 mixin(registerMethods); // once per module - don't forget!
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 rex = new Pitbull, snoopy = new Dog;
55   writeln("kick snoopy: ", kick(snoopy)); // bark
56   writeln("kick rex: ", kick(rex)); // bark and bite
57 
58   Animal felix = new Cat, flipper = new Dolphin;
59   writeln("rex meets felix: ", meet(rex, felix)); // chase
60   writeln("rex meets snoopy: ", meet(rex, snoopy)); // wag tail
61   writeln("rex meets flipper: ", meet(rex, flipper)); // ignore
62 }

Members

Aliases

MethodErrorHandler
alias MethodErrorHandler = void function(MethodError error)
Undocumented in source.
VirtualType
alias VirtualType(T : virtual!U, U) = U
Undocumented in source.

Classes

MethodError
class MethodError

Information passed to the error handler function.

virtual
class virtual(T)

Mark a parameter as virtual, and declare a method.

Enums

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

Functions

TypeIds
auto TypeIds()
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
string registerMethods(string moduleName)

Used as a string mixin: register the methods declaration and definitions in the current module.

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

Set the function that is called if a method cannot be called with the arguments. Default is to abort the program.

updateMethods
void updateMethods()

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

Mixin templates

Restrict
mixintemplate Restrict(M...)
Undocumented in source.
_method
mixintemplate _method(string name, R, A...)
Undocumented in source.
_registerSpecs
mixintemplate _registerSpecs(alias MODULE)
Undocumented in source.
implement
mixintemplate implement(alias M, alias S)
Undocumented in source.

Structs

Method
struct Method(string id, string Mptr, R, T...)
Undocumented in source.
MethodTag
struct MethodTag
Undocumented in source.
Restriction
struct Restriction
Undocumented in source.
Runtime
struct Runtime
Undocumented in source.
method
struct method

Used as an attribute: add an override to a method.

mptr
struct mptr
Undocumented in source.

Templates

CallParams
template CallParams(T...)
Undocumented in source.
VirtualArity
template VirtualArity(QP...)
Undocumented in source.
castArgs
template castArgs(T...)
Undocumented in source.

Variables

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

Meta

Authors

Jean-Louis Leroy 2017