LEARN VOID / DATA & BEHAVIOR

Enums & pattern matching

Match the shape of an outcome and name what you need.

Complete program for the synchronous entry adapter. The result below is the returned integer.

patterns.voidDownload source
enum delivery:
    pickup
    shipped(fee: i32)

func entry(shipping_fee: i32) -> i32:
    var method = delivery.pickup
    if shipping_fee > 0:
        method = delivery.shipped(shipping_fee)
    return match method:
        delivery.pickup: 0
        delivery.shipped(var charge): charge
Input 5Expected result 5

Name alternatives with an enum

A delivery is either pickup or shipped with an integer fee. The active variant determines whether there is a fee payload; there is no unrelated list or object graph.

Capture the payload explicitly

A positive input selects delivery.shipped. The match returns its fee through var charge. Otherwise pickup costs zero. Input 5 returns 5, and input zero returns zero.

Build on one pattern model

is can test the same variant shapes; var explicitly creates captures, while bare names compare existing values. Patterns can nest inside Ok/Err results. Fixed-array or value payloads copy their storage when captured; managed payloads retain identity. Incomplete matches can warn, and an unmatched match expression fails at runtime. The example covers both variants.