Pattern Matching
The strong typing paradigm of functional languages allows for powerful conditional checks over the types of each values. However Python only implements a very light version of Pattern Matching. The Xeither, Xopt, Xtry creates a unique opportunity to capitalize over this simple yet useful functionality. In their metaclass, all of those types resolves to an Xresult, which means they can all be pattern matched conjointly.
Extracting values
The main usage of Pattern matching is to check the outcome of an Xresult and extract its value at the same time.
from xfp import Xresult, Xtry
previous_result: Xresult[Exception, str] = Xtry.Success("everything's right") # mock a previous result here
match previous_result:
case Xtry.Success(value):
print(value)
case Xtry.Failure(e):
print("Something's wrong")
This can be assimilated to the Python try/except pattern, but on a more generalized level. Where the try/except is limited to errors, side effects decriptions can be encoded under any type of value, depending on your need. In this example, we can cleanly handle a multiple-exception effect :
from xfp import Xresult, Xeither
from typing import Any
previous_result: Xresult[list[Exception], Any] = Xeither.Left([Exception("e1"), Exception(["e2"])]) # mock a previous result here
match previous_result:
case Xeither.Right(value):
print(value)
case Xeither.Left([e1, *others]):
print(f"Something's wrong, the first wrong thing is {e1}")
More examples
Since Xtry, Xeither, Xopt all resolves to an Xresult, they can be used interchangeably :
from xfp import Xresult, Xtry, Xopt
previous_result: Xresult[Exception, str] = Xtry.Success("everything's right") # mock a previous result here
match previous_result:
case Xopt.Some(value):
print(value)
case Xtry.Failure(e):
print("Something's wrong")
The plain Xresult type can also be used in pattern matching :
from xfp import Xresult, XRBranch, Xtry
previous_result: Xresult[Exception, str] = Xtry.Success("everything's right") # mock a previous result here
match previous_result:
case Xresult(value, XRBranch.RIGHT):
print(value)
case Xresult(e, XRBranch.LEFT):
print("Something's wrong")