PATTERN MATCHING
Overview
In this chapter, you’ll learn how to Pony’s two types of matching work: The Match Expression and the As Operator.
Match Expressions
If we want to compare an expression to a value then we use an if. But if we want to compare an expression to a lot of values this gets very tedious. Pony provides a powerful pattern matching facility, combining matching on values and types, without any special code required. Matching: the basics Here’s a simple example of a match expression that produces a string. match x | 2 => "int" | 2.
As Operator
The as operator in Pony has two related uses. First, it provides a safe way to increase the specificity of an object’s type (casting). Second, it gives the programmer a way to specify the type of the items in an array literal. Safely converting to a more specific type (casting) The as operator can be used to create a reference to an object with a more specific type than the given reference, if possible.