Skip to content

matching

pannous edited this page Feb 20, 2023 · 15 revisions

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:

To assign a photo to a contact:
contact.image = photo

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 : 0 else fibonacci(number - 1) + fibonacci it - 2

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 : 0 else fib(it-1) + fib it - 2

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:

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,
    }
}
to get value of coin in cents:
    match coin {
        Penny : 1
        Nickel : 5
        Dime : 10
        Quarter : 25
    }

For such simple pattern matching, switch is identical to match. Use match to evaluate and match more complex expressions:

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

Home

Philosophy

data & code blocks

features

inventions

evaluation

keywords

iteration

tasks

examples

todo : bad ideas and open questions

⚠️ specification and progress are out of sync

Clone this wiki locally