diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-02-02 20:13:05 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-02-02 20:13:20 +0000 |
| commit | 89394d0fab076765b637b0fe643d8f8950639817 (patch) | |
| tree | 6852b4f81fd7f7812bb208df52b4e0cc8b1bcbdf /examples/40-struct-method-references | |
| parent | 67f010c93b11744b1f2f1c9403fd1e8d2b888acb (diff) | |
Add int arithmetic example
Diffstat (limited to 'examples/40-struct-method-references')
| -rw-r--r-- | examples/40-struct-method-references/shed-examples.json5 | 9 | ||||
| -rw-r--r-- | examples/40-struct-method-references/shed.json5 | 8 | ||||
| -rw-r--r-- | examples/40-struct-method-references/src/HelloWorld.shed | 25 |
3 files changed, 42 insertions, 0 deletions
diff --git a/examples/40-struct-method-references/shed-examples.json5 b/examples/40-struct-method-references/shed-examples.json5 new file mode 100644 index 00000000..a57c4749 --- /dev/null +++ b/examples/40-struct-method-references/shed-examples.json5 @@ -0,0 +1,9 @@ +{ + examples: [ + { + project: ".", + bin: "hello-world", + expectedOutput: "one apple\ntwo bananas\nmany coconuts!", + }, + ], +} diff --git a/examples/40-struct-method-references/shed.json5 b/examples/40-struct-method-references/shed.json5 new file mode 100644 index 00000000..a6c8e563 --- /dev/null +++ b/examples/40-struct-method-references/shed.json5 @@ -0,0 +1,8 @@ +{ + bins: [ + { + name: "hello-world", + mainNamespace: "HelloWorld", + }, + ], +} diff --git a/examples/40-struct-method-references/src/HelloWorld.shed b/examples/40-struct-method-references/src/HelloWorld.shed new file mode 100644 index 00000000..5bfd3631 --- /dev/null +++ b/examples/40-struct-method-references/src/HelloWorld.shed @@ -0,0 +1,25 @@ +pub fun main() ! Io -> Unit { + val one = Count(1).message; + val two = Count(2).message; + val three = Count(3).message; + + print!(one("apple", .exclaim = false)); + print!(two("bananas", .exclaim = false)); + print!(three("coconuts", .exclaim = true)); +} + +struct Count(value: Int) { + fun message(this, noun: String, .exclaim: Bool) -> String { + val prefix = if (this.value == 1) { + "one" + } else if (this.value == 2) { + "two" + } else { + "many" + }; + + val exclamation = if (exclaim) { "!" } else { "" }; + + prefix + " " + noun + exclamation + "\n" + } +} |
