This library provides a limited form of "generic functions". Generic functions are a concept found in other languages (native to Common Lisp and Dylan, available as extension module in Python).
The major differences between a "generic function" (as defined in this library) and a regular function are
-
generic functions dispatch to the appropriate implementation based on the concrete run-time type of the arguments, whereas Java's method overloading is a purely syntactic compile-time feature.
-
all arguments are considered for the dispatch, whereas in a standard method call, only the implicit receiver argument has any influence over the method look up.
This allows us to define code like:
static void serialize(Object object, Object stream) {
serializer.accept(object, stream);
}
static final GenericBiConsumer serializer = new GenericBiConsumer("serialize");
static {
serializer.addMethod(FancyId.class, XmlStream.class, (object, writer) -> { ... });
serializer.addMethod(FancyId.class, BinaryStream.class, (object, writer) -> { ... });
...
}