You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
👋 Hi there. Great work on this library! Do you have some thoughts on supporting pattern matching (upcoming feature in Python 3.10)? I can foresee a few issues if we don't give this some thought.
The 'default' approach would probably be something like:
matchmyvalue:
caseSome(6): print('six!')
caseSome(x): print(x)
caseNothing: print("nothing to see here")
(This currently does not work, but would probably be possible to implement without changes to the API)
The problem is the last case. It only appears like it only matches Nothing instances. In actuality, it matches anything. Misspelling it asNohting would pass the type checker as well! This is because python handles case <any variable name>: as the 'fallback' case.
Problem? You cannot handle the Nothing case first:
matchmyvalue:
caseNothing: print('nothing to see here') # python interprets this as the 'fallback' casecaseSome(x): print(x) # cases after the 'fallback case' are not allowed!
File "foo.py", line 6
case Nothing: print('nothing to see here')
^^^^^^^
SyntaxError: name capture 'Nothing' makes remaining patterns unreachable
There are a few ways to address this:
option 1: Do not address this. In the end the functionality works...sort of
problems:
I. It's confusing because pattern matching is not actually occurring
II. You don't allow matching on Nothing first
III. You get problems when you want to handle more cases. It's a bit contrived, but you could want to handle myvalue of type Union[list, Maybe[int]]. In that case you could not:
matchmyvalue:
caseSome(x): print(x)
caseNothing: print('nothing to see here') # this is handled as the fallback case and ruins everythingcase []: print('empty list')
case _: print('something random')
You would have to explain to everyone that Nothing must be matched last
option 2: Follow the advice of PEP636
Patterns may use named constants. These must be dotted names to prevent them from being interpreted as capture variable:
matchmyvalue:
casemaybe.Nothing: print('nothing to see here')
casemaybe.Some(x): print(x)
This is a bit clunky though... 🤔
Option 3: Somehow enable Nothing() as a pattern match:
matchmyvalue:
caseNothing(): print('nothing to see here')
caseSome(x): print(x)
This is confusing because this usage is different from using Nothing in any other context. Other alternative is to break the API by mandating Nothing() instead of Nothing (not something you'd like, I think)
Option 4: something I'm not thinking of
I probably missed another option, but I'm curious what you feel is the best way going forward.
The text was updated successfully, but these errors were encountered:
👋 Hi there. Great work on this library! Do you have some thoughts on supporting pattern matching (upcoming feature in Python 3.10)? I can foresee a few issues if we don't give this some thought.
The 'default' approach would probably be something like:
(This currently does not work, but would probably be possible to implement without changes to the API)
The problem is the last case. It only appears like it only matches
Nothing
instances. In actuality, it matches anything. Misspelling it asNohting
would pass the type checker as well! This is because python handlescase <any variable name>:
as the 'fallback' case.Problem? You cannot handle the
Nothing
case first:There are a few ways to address this:
option 1: Do not address this. In the end the functionality works...sort of
problems:
I. It's confusing because pattern matching is not actually occurring
II. You don't allow matching on
Nothing
firstIII. You get problems when you want to handle more cases. It's a bit contrived, but you could want to handle
myvalue
of typeUnion[list, Maybe[int]]
. In that case you could not:You would have to explain to everyone that
Nothing
must be matched lastoption 2: Follow the advice of PEP636
This is a bit clunky though... 🤔
Option 3: Somehow enable
Nothing()
as a pattern match:This is confusing because this usage is different from using
Nothing
in any other context. Other alternative is to break the API by mandatingNothing()
instead ofNothing
(not something you'd like, I think)Option 4: something I'm not thinking of
I probably missed another option, but I'm curious what you feel is the best way going forward.
The text was updated successfully, but these errors were encountered: