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

_isNamedSpec
eponymoustemplate _isNamedSpec(alias spec)
Undocumented in source.
hasVirtualParameters
eponymoustemplate hasVirtualParameters(alias F)
Undocumented in source.

Functions

_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
MethodErrorHandler setMethodErrorHandler(MethodErrorHandler 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.

trace
void trace(T args)
Undocumented in source. Be warned that the author may not have intended to support it.
tracef
void tracef(T args)
Undocumented in source. Be warned that the author may not have intended to support it.
tracefln
void tracefln(T args)
Undocumented in source. Be warned that the author may not have intended to support it.
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.

Manifest constants

MptrInDeallocator
enum MptrInDeallocator;
Undocumented in source.
MptrViaHash
enum MptrViaHash;
Undocumented in source.

Mixin templates

Registrar
mixintemplate Registrar(TheMethod, alias Spec)
Undocumented in source.
_registerSpecs
mixintemplate _registerSpecs(alias MODULE)
Undocumented in source.
registerClasses
mixintemplate registerClasses(Classes...)
Undocumented in source.

Structs

Method
struct Method(string Mptr, R, string ID, uint FA, string[] SCM, 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

MethodOf
template MethodOf(alias Fun)
Undocumented in source.
_specId
template _specId(alias M, alias spec)
Undocumented in source.
_specId
template _specId(alias M, alias spec)
Undocumented in source.
staticSlice
template staticSlice(alias Pack, alias Positions)
Undocumented in source.

Variables

errorHandler
MethodErrorHandler errorHandler;
Undocumented in source.

Meta

Authors

Jean-Louis Leroy 2017