|
|
|
|
|
by d0mine
1479 days ago
|
|
All these lambdas seem excessive. "Maybe"-like pattern in Python can look like (None is falsy and custom objects are true): # note: `None and f()` returns None without calling f
balance = user and user.get_balance()
credit = balance and balance.credit_amount()
discount_program = choose_discount(credit) if credit and credit > 0 else None
https://stackoverflow.com/questions/8507200/maybe-kind-of-mo... |
|
Optional[Foo] is same as Foo | None, meaning you can operate on foo with Foo's methods, except that if it's None, you get NoneType errors.
Maybe[Foo] is an actual container. You have to map over or unwrap it to operate on Foo.
The big difference is ergonomics/opinion. You can't actually map over Optional, so every time you wanna use it, you have to manually check if foo is not None. Whereas Maybe, you can operate generically over it. Some folks think "is not None" is better. Personally I hate NoneType errors in prod and find that much more painful than a bit of indirection.