Skip to content

Patterns

Avarel edited this page Sep 17, 2017 · 1 revision

In progress.

  • Any pattern, always matches: _
  • Value pattern, matches exact value: 1 []
  • Named patterns, patterns with names
    • Variable pattern, matches positional/named value: x
    • Rest pattern, always matches: ...x
  • Default pattern, always matches: (named pattern) = value
  • Tuple pattern, matches/destructures tuples
    • Any match, match if x exists: x: _
    • Value match, match if x is 1: x: 1
    • Variable match, match x and assign to a variable x: a
    • Nested match, match internal contents of x: x: (a, 2)

Combine, mix match at assignment sites:

let a, b = 1, 2
let (c, ...d, e) = 1, 2, 3, 4 , 5

a, b = b, a

They are also the core behind function patterns. (Multimethods coming soon)

def function(x, ...y, z = 5) {
    println "$x $y $z"
}

Clone this wiki locally