Skip to content

Commit

Permalink
Allow positional pattern matching via __match_args__
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhammerl committed Oct 8, 2021
1 parent 0a274ee commit 5a4a639
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,18 @@ For those running Python 3.10, you can make use of Python's **structural pattern

```
>>> from resultify import Ok, Err
>>> ok = Ok('ok!')
>>> ok = Ok("ok!")
>>> match ok:
... case Ok(_value=foo): print(f"Yay {foo}")
... case Err(_value=foo): print(f"Nay {foo}")
... case Ok(foo): print(f"Yay {foo}")
... case Err(foo): print(f"Nay {foo}")
...
Yay ok!
>>> no = Err("nope!")
>>> match no:
... case Ok(foo): print(f"Yay {foo}")
... case Err(foo): print(f"Nay {foo}")
...
Nay nope!
```


Expand Down
4 changes: 4 additions & 0 deletions resultify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Ok(Generic[T]):
A value that indicates success and which stores arbitrary data for the return value.
"""

__match_args__ = ("_value",)

def __init__(self, value: T = True) -> None:
self._value = value

Expand Down Expand Up @@ -50,6 +52,8 @@ class Err(Generic[E]):
A value that signifies failure and which stores arbitrary data for the error.
"""

__match_args__ = ("_value",)

def __init__(self, value: E = True) -> None:
self._value = value

Expand Down

0 comments on commit 5a4a639

Please sign in to comment.