From fa2bd5fe73d359364e7b897f438a84bcd85fb5ed Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 8 Oct 2023 20:50:40 +0100 Subject: Add sums example --- examples/15-sums/shed.toml | 9 +++++++++ examples/15-sums/src/HelloWorld.shed | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 examples/15-sums/shed.toml create mode 100644 examples/15-sums/src/HelloWorld.shed (limited to 'examples') diff --git a/examples/15-sums/shed.toml b/examples/15-sums/shed.toml new file mode 100644 index 00000000..3a2d316f --- /dev/null +++ b/examples/15-sums/shed.toml @@ -0,0 +1,9 @@ +[shed] +main = "HelloWorld" + +[[shed.bins]] +name = "hello-world" + +[[shed.examples]] +bin = "hello-world" +expected-output = "Some(hello, world!)\nNone" diff --git a/examples/15-sums/src/HelloWorld.shed b/examples/15-sums/src/HelloWorld.shed new file mode 100644 index 00000000..3dd7038d --- /dev/null +++ b/examples/15-sums/src/HelloWorld.shed @@ -0,0 +1,23 @@ +struct Some(.value: String) {} + +struct None() {} + +sumtype Option = Some | None; + +fun main() ! Io -> Unit { + printOption!(Some(.value = "hello, world!")); + printOption!(None()); +} + +fun printOption(option: Option) !Io -> Unit { + when (option) { + is Some { + print!("Some("); + print!(option.value); + print!(")\n"); + } + is None { + print!("None\n"); + } + } +} -- cgit v1.2.3