MoonBit Language Tour MoonBit

Map Pattern

MoonBit provides convenient pattern matching for map-like data structures. In a map pattern { key1: pattern1, key2?: pattern2, .. }:

  • The key1: pattern1 part matches if the key exists in the map and the value associated with the key matches the specified pattern.

  • The key2?: pattern2 part matches regardless of whether the key exists; in this case, pattern2 will match against an option value. If the key2 exists in the map, this value will be Some(value), otherwise it will be None.

  • The .. part indicates that the pattern can match maps with additional keys not specified in the pattern. This marker is always required in map patterns.

The example shows how to process user configuration settings using map pattern matching. The process_config function handles various configurations and provides appropriate defaults when certain keys are missing.

///|
fn process_config(config : Map[String, String]) -> String {
  match config {
    // Pattern 1: Must have "name" key, optional "theme" key
    { "name": username, "theme"? : Some(theme), .. } =>
      "Welcome \{username}! Using \{theme} theme."

    // Pattern 2: Must have "name" key, theme is missing
    { "name": username, "theme"? : None, .. } =>
      "Welcome \{username}! Using default theme."

    // Pattern 3: Missing required "name" key
    { "theme": theme, .. } => "Guest user with \{theme} theme."

    // Pattern 4: Empty or other configurations
    _ => "Guest user with default settings."
  }
}

///|
fn main {
  // Test different configurations
  let config1 = { "name": "Alice", "theme": "dark", "language": "en" }
  let config2 = { "name": "Bob", "language": "zh" }
  let config3 = { "theme": "light", "timeout": "30" }
  let config4 = {}
  println(process_config(config1))
  println(process_config(config2))
  println(process_config(config3))
  println(process_config(config4))
}