diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-12-22 14:20:02 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-12-22 14:20:02 +0000 |
| commit | 92c93593a08ad33a506c4feb3cc58e6b0953b8e6 (patch) | |
| tree | 079ec62ad902d1a236a852a91920bbd4f537d9cb /examples/36-struct-method-references/src/HelloWorld.shed | |
| parent | 1397d18e897e98ede05e8bbc7b394c0469fb887a (diff) | |
Add example for method references
Diffstat (limited to 'examples/36-struct-method-references/src/HelloWorld.shed')
| -rw-r--r-- | examples/36-struct-method-references/src/HelloWorld.shed | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/examples/36-struct-method-references/src/HelloWorld.shed b/examples/36-struct-method-references/src/HelloWorld.shed new file mode 100644 index 00000000..5bfd3631 --- /dev/null +++ b/examples/36-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" + } +} |
