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   import std.stdio;
51 
52   Animal hector = new Pitbull, snoopy = new Dog;
53   writeln("kick snoopy: ", kick(snoopy)); // bark
54   writeln("kick hector: ", kick(hector)); // bark and bite
55 
56   Animal felix = new Cat, flipper = new Dolphin;
57   writeln("hector meets felix: ", meet(hector, felix)); // chase
58   writeln("hector meets snoopy: ", meet(hector, snoopy)); // wag tail
59   writeln("hector meets flipper: ", meet(hector, flipper)); // ignore
60 }

Members

Aliases

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

Classes

MethodError
class MethodError

Information passed to the error handler function.

Enums

hasVirtualParameters
eponymoustemplate hasVirtualParameters(alias F)
Undocumented in source.

Functions

defaultMethodErrorHandler
void defaultMethodErrorHandler(MethodError error)
Undocumented in source. Be warned that the author may not have intended to support it.
makeCallParams
auto makeCallParams(rf.Parameter[] parameters)
Undocumented in source. Be warned that the author may not have intended to support it.
next
auto ref 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.

registrationMixture
string registrationMixture()
Undocumented in source. Be warned that the author may not have intended to support it.
removeStorageClasses
auto removeStorageClasses(rf.Parameter[] parameters)
Undocumented in source. Be warned that the author may not have intended to support it.
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 after dynamically loading or unloading a shared library.

Interfaces

Registrar
interface Registrar
Undocumented in source.

Manifest constants

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

Mixin templates

registerClasses
mixintemplate registerClasses(Classes...)
Undocumented in source.
registrar
mixintemplate registrar(alias MODULE, alias ModuleName)
Undocumented in source.

Structs

Method
struct Method(alias module_, string name, int index)
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.

Variables

errorHandler
MethodErrorHandler errorHandler;
Undocumented in source.

Meta

Authors

Jean-Louis Leroy