Gauntlet Documentation
  • 🏠Welcome
  • πŸ…Getting Started
    • 🧠Introduction To Gauntlet
  • ‼️Read before Proceeding
  • ⬇️Installation
  • πŸ‘¨β€πŸ’»VSCode Extension
  • πŸ“šBasics
    • πŸ’¨Running Gauntlet
    • πŸ“„File Setup & Hello World
  • πŸ” Scope Variables
  • πŸ–ΌοΈConstants
  • 🧩Functions
  • ↔️If Statements
  • πŸ”‘Ternary Operator
  • πŸ’ Switch-Case
  • πŸ“©Select-Case
  • ➰Loops
  • πŸ“Structs
  • 🧱Interfaces
  • πŸͺͺAliases
  • πŸ“ŽMethods
  • πŸ¦™Lambdas
  • πŸ•ΈοΈMiscellaneous
  • ⚑Advanced Features
    • πŸ”€When-Cases
    • πŸš₯Tagged Unions
    • 🎚️Pattern Matching
    • 🚰Pipes
    • ⁉️Try-Statements
    • 🎭Force-Statements
    • 🌯Wrapper Types
  • πŸ”ΌVersion Release Notes
    • v0.2.0-alpha
Powered by GitBook
On this page
  • Syntax
  • Working Example
Export as PDF
  1. Advanced Features

Pattern Matching

Syntax

match <expr> {
    case <case expr>: {
        <code block>
    }
}

<case expr> refers to any of the following:

  • An enum literal (e.g. Circle() in the below example)

    • Currently, you cannot pattern match on any field that is another enum literal. Support is currently being implemented for this.

  • A string (e.g. "hello")

  • An int (e.g. 30)

  • A decimal (e.g. 5.0)

  • A boolean (e.g. true)

  • A char (e.g. 'a')

  • [Support Coming Soon] Wildcard

  • [Support Coming Soon] Array literal (e.g. [2]string{"hello", "world"})

  • [Support Coming Soon] Slice literal (e.g. []string{"hello", "world"})

Working Example

package main

import "fmt" as fmt
import "strconv" as strconv

enum Shapes {
    Circle(diameter: Int, color: String)
    Square(width: Int, height: Int, color: String)
    Triangle(base: Int, width: Int, height: Int, color: String)
}

fun main(): Unit {
    fmt.println("I only like red triangles. Let's see...")
    
    let myShape = Triangle(base: 10, width : 5, height : 20, color : "red")
    
    match myShape {
        case Triangle(base: let b, height: let height, color : "red") : {
            fmt.println("It's a red triangle with a height of " + strconv.itoa(height) + " and base length of " + strconv.itoa(b) + "!")
        }
        case Square(width : let width, color : "red") : {
            fmt.println("It's a red square with a width of " + strconv.itoa(width))
        }
        case Circle(diameter: let diameter, color : "red") : {
            fmt.println("It's a red circle with a diameter of " + strconv.itoa(diameter))
        }
    }
}
package main

import fmt "fmt"
import strconv "strconv"

type Case343818345 struct {
diameter int
color    string
}

type Case421059818 struct {
width  int
height int
color  string
}

type Case499594069 struct {
base   int
width  int
height int
color  string
}

func main() {
fmt.Println("I only like red triangles. Let's see...")
myShape := map[int]any{499594069: Case499594069{base: 10, width: 5, height: 20, color: "red"}}
if _, exists := myShape[499594069]; exists {

	if height, b := myShape[499594069].(Case499594069).height, myShape[499594069].(Case499594069).base; (func() bool {
		return (myShape[499594069].(Case499594069).base == (b)) && (myShape[499594069].(Case499594069).height == (height)) && (myShape[499594069].(Case499594069).color == ("red"))
	})() {
		fmt.Println("It's a red triangle with a height of " + strconv.Itoa(height) + " and base length of " + strconv.Itoa(b) + "!")
	}
}
if _, exists := myShape[421059818]; exists {

	if width := myShape[421059818].(Case421059818).width; (func() bool {
		return (myShape[421059818].(Case421059818).width == (width)) && (myShape[421059818].(Case421059818).color == ("red"))
	})() {
		fmt.Println("It's a red square with a width of " + strconv.Itoa(width))
	}
}
if _, exists := myShape[343818345]; exists {

	if diameter := myShape[343818345].(Case343818345).diameter; (func() bool {
		return (myShape[343818345].(Case343818345).diameter == (diameter)) && (myShape[343818345].(Case343818345).color == ("red"))
	})() {
		fmt.Println("It's a red circle with a diameter of " + strconv.Itoa(diameter))
	}
}
// Eliminates any 'unused variable' errors
_ = myShape

}
I only like red triangles. Let's see...
It's a red triangle with a height of 20 and base length of 10!

PreviousTagged UnionsNextPipes

Last updated 1 day ago

⚑
🎚️