Sum Types
A "sum" type is the opposite of a "product" type. This Python object is an example of a product type:
man.studies_finance = True
man.has_trust_fund = FalseThe total number of combinations a man can have is 4, the product of 2 * 2:
True
True
True
False
False
True
False
False
If we add a third attribute, perhaps a has_blue_eyes boolean, the total number of possibilities multiplies again, to 8!
True
True
True
True
True
False
True
False
True
True
False
False
False
True
True
False
True
False
False
False
True
False
False
False
But let's pretend that we live in a world where there are really only three types of people that our program cares about:
Dateable
Undateable
Maybe dateable
We can reduce the number of cases our code needs to handle by using a (admittedly fake Pythonic) sum type with only 3 possible types:
Then we can use the isinstance built-in function to check if a Person is an instance of one of the subclasses. It's a clunky way to represent sum types, but hey, it's Python.
Sum Types
As opposed to product types, which can have many (often infinite) combinations, sum types have a fixed number of possible values. To be clear: Python doesn't really support sum types. We have to use a workaround and invent our own little system and enforce it ourselves.
Assignment
Whenever a document is parsed by Doc2Doc, it can either succeed or fail. In functional programming, we often represent errors as data (e.g. the ParseError class) rather than by raiseing exceptions, because exceptions are side effects. (This isn't standard Python practice, but it's useful to understand from an FP perspective)
Complete the Parsed and ParseError subclasses.
Parsedrepresents success. It should accept adoc_namestring and atextstring and save them as properties of the same name.ParseErrorrepresents failure. It should accept adoc_namestring and anerrstring and save them as properties of the same name.
The test suite uses the isinstance function to see if an error occurred based on the class type.
Solution:
Why Does This Work?
This works because it correctly follows object-oriented principles while staying true to a functional programming (FP) approach to error handling. Hereβs why:
β 1. Inheritance Helps Categorize Outcomes
ParsedandParseErrorinherit fromMaybeParsed, meaning they share a common parent.This allows us to easily check whether an object represents a parsing result using
isinstance().
β 2. Each Class Stores Its Own Data Correctly
Parsedsaves successful results:It stores the document name (
doc_name) and its extracted text (text).
ParseErrorsaves failure information:Instead of
text, it storeserr, which describes the reason for failure.
β 3. Functional Programming Approach
Instead of throwing exceptions, we return an instance of either
ParsedorParseErrorto represent success or failure.This makes it easy to handle results using pattern matching or conditional checks.
Example Usage: Handling Success and Failure
Output:
Key Takeaways
We avoid exceptions. Instead of
raise Exception("Parsing failed"), we use structured data to represent errors.We use
isinstanceto distinguish success and failure. This makes error handling explicit.We keep the data encapsulated in objects. Instead of returning just strings (
"Success"or"Error: file not found"), we return structured objects with attributes (doc_name,text,err).This pattern is common in functional languages like Haskell and Scala. It mimics
Resulttypes found in FP languages.
Last updated