Ovld – Efficient and featureful multiple dispatch for Python
a year ago
- #type-annotations
- #python
- #multiple-dispatch
- ovld is a fast multiple dispatch library for Python that allows defining functions for different type signatures using annotations.
- It supports multiple arguments, unlike Python's singledispatch, and includes features like variants, mixins, and medleys of functions and methods.
- ovld allows for dependent types, enabling overloaded functions to depend on actual values, not just types.
- It can dispatch on functions, methods, positional and keyword arguments, and supports code generation for advanced use cases.
- The library is particularly useful for recursive definitions, such as tree maps or serialization, with the `recurse` function for recursive calls.
- Variants of ovld functions can be created to specialize behavior, such as changing addition to multiplication in a recursive function.
- Priority can be assigned to methods, allowing for wrapping calls with generic behavior or debugging.
- Dependent types are supported through `Literal` and `Dependent`, enabling functions like factorial that depend on value conditions.
- Custom types can be defined with the `@dependent_check` decorator, useful for libraries like PyTorch.
- Multiple dispatch on methods is possible by inheriting from `OvldBase` or using the `OvldMC` metaclass, with support for subclassing and extending methods.
- Medleys allow combining functionality from different classes into a new class, supporting dataclass fields and method merging.
- ovld is significantly faster than other multiple dispatch libraries, with benchmarks showing it to be 1.5x to 100x faster in various scenarios.