Skip to content

Conversation

gdotdesign
Copy link
Member

@gdotdesign gdotdesign commented Mar 27, 2025

This PR adds support for records in pattern matching and an update to pattern matching itself.


Fields can be matched with the same syntax as records:

type User {
  name: String,
  age: Number
}

case { name: "John", age: 42 } {
  { name: name, age: 42 } => name
  => "Unkown"
}

The other change is that patterns are now limited to the following ones:

  • Type: Maybe(...)
  • Record: {...}
  • Tuple: {a, b}
  • Array: [...]
  • Variable: a
  • String literal (without interpolation): "a"
  • Number literal: 0.1
  • Bool literal: true

It's needed, so exhaustiveness checking works reliably, for example:

type SomeType {
  A
  B
}

fun test(value: SomeType, otherValue: SomeType) {
  case value {
    // if otherValue here is the variable above
    otherValue => "X"
    SomeType.A => "A"
    SomeType.B => "B"
  }
}

In the example above, we can't be sure which branch is redundant, since we don't know the value of otherValue at compile time.

I'll try to do explore how to enable this in the future. Elixir has it, but they don't support exhaustiveness, Gleam, Rust and Elm don't allow this, not sure if other languages do. If anyone has research on it, let me know :)

@gdotdesign gdotdesign added the language Language feature label Mar 27, 2025
@gdotdesign gdotdesign added this to the 0.24.0 milestone Mar 27, 2025
@gdotdesign gdotdesign self-assigned this Mar 27, 2025
@gdotdesign gdotdesign force-pushed the record-pattern-matching branch 2 times, most recently from fdc881f to 0a2d20d Compare March 27, 2025 14:55
@gdotdesign gdotdesign marked this pull request as ready for review March 27, 2025 15:21
@gdotdesign gdotdesign requested a review from Sija March 27, 2025 15:21
@gdotdesign gdotdesign force-pushed the record-pattern-matching branch from 0a2d20d to f7e70af Compare April 3, 2025 13:06
@gdotdesign gdotdesign merged commit 1ccf8ab into master Apr 3, 2025
3 checks passed
@gdotdesign gdotdesign deleted the record-pattern-matching branch April 3, 2025 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language Language feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant