matching - pannous/warp GitHub Wiki
Angle has different types of matching
type matching
see type inference
Match by type name:
Merge the concept of type and variable (name) in simple functions:
class contact{name:text; image:text}
to assign a photo to a contact: contact.image = photo
c = contact("Ann", "")
assign "me.png" to c
c.image // me.png
Here both photo and contact act as matching types and as variable identifiers. What sounds pretty complicated is very obvious in code (see above).
Fibonacci in Angle is
fibonacci number = if number<2 : number else fibonacci(number - 1) + fibonacci(number - 2)
fibonacci 10 // 55
Note how number simulataneously acts as type and variable name. This invention is called matching by
type name. Note how the unique argument number can be accessed via it keyword and brackets are optional
Auto typed fibonacci in Angle is
fib := if it<2 : it else fib(it-1) + fib(it-2)
fib 10 // 55
A typical class record with optional fields can be expressed like this
class contact {
name
email?
phone?
address {
street
city
zip?
}
}
Again we have the concept of matching by type name: The field email is inferred to have type Email
Usually if fields of the same type occur more than once there are natural distinguishing aspects (planned, card multi-word-fields):
class full name{
first name
last name
}
However in some situations one may chose to disconnect the variable name from its type:
class foo{
bar name
buz name
box name
}
Todo: In case of future multi-word class names without hyphones: How to avoid undesired matching of class names from different contexts.
switch pattern matching
To match a list of items against criteria, we could adopt the beautiful Rust syntax:
https://doc.rust-lang.org/std/keyword.match.html
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
In warp:
enum Coin { Penny, Nickel, Dime, Quarter }
to get value of coin in cents:
match coin {
Penny : 1
Nickel : 5
Dime : 10
Quarter : 25
}
cents = get value of Dime in cents
cents // 10
switch is a synonym of match. Keys made of values, alternatives, ranges and types: switch; lists, tuples, maps
and enum cases with values: pattern-matching.
Other pattern matching implementations: C# ugly java swift grain rust
variant matching
enums aka variants are aliases which are only needed for interop, otherwise all node_pointer are prone to matching