diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-29 11:52:18 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-29 11:52:18 +0000 |
| commit | f1bd514636600b355d7656007a0c823e5e868ca8 (patch) | |
| tree | 2fac06a23df677d50eac5f413a905e25905fa508 /examples | |
| parent | 2ac984f788b266bea2bc27081a84788203f1e108 (diff) | |
Add generic sums example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/30-generic-sums/shed.toml | 7 | ||||
| -rw-r--r-- | examples/30-generic-sums/src/HelloWorld.shed | 23 |
2 files changed, 30 insertions, 0 deletions
diff --git a/examples/30-generic-sums/shed.toml b/examples/30-generic-sums/shed.toml new file mode 100644 index 00000000..842e1e9c --- /dev/null +++ b/examples/30-generic-sums/shed.toml @@ -0,0 +1,7 @@ +[[shed.bins]] +name = "hello-world" +main-namespace = "HelloWorld" + +[[shed.examples]] +bin = "hello-world" +expected-output = "Some(hello, world!)\nNone" diff --git a/examples/30-generic-sums/src/HelloWorld.shed b/examples/30-generic-sums/src/HelloWorld.shed new file mode 100644 index 00000000..314546b5 --- /dev/null +++ b/examples/30-generic-sums/src/HelloWorld.shed @@ -0,0 +1,23 @@ +struct Some[T](.value: T) {} + +struct None() {} + +sumtype Option[T] = Some[T] | None; + +pub fun main() ! Io -> Unit { + printOption!(Some[String](.value = "hello, world!")); + printOption!(None()); +} + +fun printOption(option: Option[String]) !Io -> Unit { + when (option) { + is Some[String] { + print!("Some("); + print!(option.value); + print!(")\n"); + } + is None { + print!("None\n"); + } + } +} |
