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
A common mistake I see people who are new to pattern matching make is to see patterns that match on literals and expect to be able to match on variables the same way. For example:
@data Shape beginCircle(radius)
Rectangle(height, width)
endfunctionwhose_circle_is_this(circle)
my_circle =Circle(1)
@match circle begin
my_circle =>print("this is my circle")
Circle(radius) =>print("this is someone else's circle")
_ =>print("this isn't a circle at all")
endend
The person who wrote this might be surprised to see this result:
julia> whose_circle_is_this(Rectangle(1, 2))
this is my circle
Other implementations of pattern matching prevent this class of error by checking for patterns that make subsequent patterns unreachable. For example, in Python:
classShape:
passclassCircle(Shape):
def__init__(self, radius):
self.radius=radiusclassRectangle(Shape):
def__init__(self, width, height):
self.width=widthself.height=heightdefwhose_circle_is_this(circle):
my_circle=Circle(1)
matchcircle:
casemy_circle:
print("this is my circle")
caseCircle(radius):
print("this is someone else's circle")
case _:
print("this isn't a circle at all")
Trying to define that last function throws the following syntax error:
File "<python-input-10>", line 4
case my_circle:
^^^^^^^^^
SyntaxError: name capture 'my_circle' makes remaining patterns unreachable
which is pretty helpful to protect against that pretty common error. I suppose it might even be enough to check that a generic catch-all name capture doesn't have any patterns following it.
The text was updated successfully, but these errors were encountered:
A common mistake I see people who are new to pattern matching make is to see patterns that match on literals and expect to be able to match on variables the same way. For example:
The person who wrote this might be surprised to see this result:
Other implementations of pattern matching prevent this class of error by checking for patterns that make subsequent patterns unreachable. For example, in Python:
Trying to define that last function throws the following syntax error:
which is pretty helpful to protect against that pretty common error. I suppose it might even be enough to check that a generic catch-all name capture doesn't have any patterns following it.
The text was updated successfully, but these errors were encountered: