Ovld is a Python library that provides fast and feature-rich multiple dispatch for functions using type annotations. Unlike Python's built-in `functools.singledispatch`, `ovld` supports dispatching on multiple arguments, custom predicates, and value-based dispatch. It aims to simplify code that would otherwise rely on complex `if-elif` chains or `isinstance` checks for different argument types. The library is actively maintained, with the current version being 0.5.15, and offers performance superior to other multiple dispatch libraries.
pip install ovldVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic function overloading for different types and argument counts. It also includes an example of recursive dispatch using `recurse`, which is essential for ensuring that nested calls respect `ovld`'s dispatch mechanism and any defined variants.
Replace direct recursive calls (e.g., `my_func(a, b)`) with `recurse(a, b)` inside `@ovld` decorated functions.
Ensure the first argument to `Dependent` is a valid Python type that acts as a bound, and the second argument is a callable predicate that operates on the argument's value.
Be aware of this limitation and implement additional runtime checks if strict validation of all elements in generic collections is required, or define more specific overloads.
If intermediate results need processing, wrap the `recurse()` calls explicitly or define custom dispatch logic instead of relying solely on `postprocess`.
To resolve this, define a more specific `@ovld` overload that exactly matches the ambiguous call's argument types, or refine existing overloads to ensure a clear hierarchy and specificity.
Verify the spelling of `ovld` and other components you are trying to import (e.g., `OvldMC`). The main decorator is typically `ovld` and the metaclass is `OvldMC`.
Inspect the arguments being passed and the `@ovld` definitions. Either adjust the arguments to conform to an existing overload's signature, or define a new `@ovld` overload with a signature (type annotations and/or predicates) that correctly matches the argument types being provided.
Instead of `isinstance(obj, list[int])`, use `typing.get_origin()` and `typing.get_args()` to inspect generic types at runtime if you need to check both the base type and its parameters. Alternatively, leverage `ovld`'s built-in support for generic collections which handles checks like `list[str]` by inspecting the first element or overall type.
No dependency data recorded yet.