From 92b7decacc011ade08a617bab678ff2bff23bf1f Mon Sep 17 00:00:00 2001 From: jstcz Date: Tue, 18 Aug 2026 15:54:42 +0200 Subject: [PATCH 1/2] Add pattern-matching example in playground --- playground/src/examples/index.ts | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/playground/src/examples/index.ts b/playground/src/examples/index.ts index d54101c6..6c82e841 100644 --- a/playground/src/examples/index.ts +++ b/playground/src/examples/index.ts @@ -118,6 +118,72 @@ function second(pair: Pair) returns (b) { function main() returns (word) { return second(Pair(true, 42)); } +`, + }, + ], + }, + { + id: "pattern-matching", + name: "Pattern matching", + description: "An escrow whose lifecycle is an enum stored in a contract field, driven by match.", + entry: "main.sol", + files: [ + { + path: "main.sol", + content: `import * from std; +import * from std.dispatch; +import * from std.Generic; +import * from std.StorageGeneric; + +// The escrow's lifecycle is a sum type: each phase is a constructor, and the +// funded/released phases carry the amount as a payload. Storing \`Phase\` in a +// contract field works because enums derive a storage representation. +enum Phase { + AwaitingPayment, + Funded(uint256), + Released(uint256) +} + +contract Escrow { + phase: Phase; + + constructor() { + phase = Phase.AwaitingPayment; + } + + function deposit(amount: uint256) public { + match (phase) { + case Phase.AwaitingPayment { + phase = Phase.Funded(amount); + } + default { + require(false, "already funded"); + } + } + } + + function release() public returns (uint256) { + match (phase) { + case Phase.Funded(amount) { + phase = Phase.Released(amount); + return amount; + } + default { + require(false, "nothing to release"); + return uint256(0); + } + } + } + + // 0 = awaiting payment, 1 = funded, 2 = released + function status() public returns (uint256) { + match (phase) { + case Phase.AwaitingPayment { return uint256(0); } + case Phase.Funded(_) { return uint256(1); } + case Phase.Released(_) { return uint256(2); } + } + } +} `, }, ], From 85cfc19a4b184430c3cb379e446216c7a82ec2d9 Mon Sep 17 00:00:00 2001 From: jstcz Date: Tue, 18 Aug 2026 23:38:49 +0200 Subject: [PATCH 2/2] Tighten pattern-matching example comments --- playground/src/examples/index.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/playground/src/examples/index.ts b/playground/src/examples/index.ts index 6c82e841..42db10d3 100644 --- a/playground/src/examples/index.ts +++ b/playground/src/examples/index.ts @@ -135,9 +135,7 @@ import * from std.dispatch; import * from std.Generic; import * from std.StorageGeneric; -// The escrow's lifecycle is a sum type: each phase is a constructor, and the -// funded/released phases carry the amount as a payload. Storing \`Phase\` in a -// contract field works because enums derive a storage representation. +// An escrow whose lifecycle is a sum type stored in a contract field. enum Phase { AwaitingPayment, Funded(uint256),