Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion docs/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class Real:

## Decorators

You can use `@dispatch` with other decorators without any problem:
You can use `@dispatch` together with other decorators, provided the decorators are ordered correctly.
When combining `@dispatch` with other decorators (for example, `@staticmethod`, `@classmethod`, or `@property`), `@dispatch` should generally be the **inner** decorator (i.e., closest to the function definition) for dispatch to work correctly:

```python
from plum import dispatch
Expand Down Expand Up @@ -57,6 +58,32 @@ class MyClass:
NotFoundLookupError: For function `name` of `__main__.MyClass`, `(<__main__.MyClass object at 0x7f8cb8813eb0>, 1)` could not be resolved.
```

For example, when using `@staticmethod`, `@dispatch` should be the inner decorator (i.e., `@dispatch` wraps the function first):

```python
from plum import dispatch


class MyClass:
@staticmethod
@dispatch
def my_func(a: int, b: int) -> int:
return a + b

@staticmethod
@dispatch
def my_func(a: float, b: float) -> float:
return a + b
```

```python
>>> MyClass.my_func(1, 2)
3

>>> MyClass.my_func(1.0, 2.0)
3.0
```


(forward-references)=
## Forward References
Expand Down