louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Labelled and unlabelled arguments mix (ADR 0061): labels are declared |
| 2 | // at the definition and apply by name in any order; unlabelled ones fill |
| 3 | // the unlabelled parameters in order — so `use` works with labels. |
| 4 | |
| 5 | import std/io |
| 6 | |
| 7 | fn guard :: |
| 8 | ~when: Bool |
| 9 | ~value: String |
| 10 | otherwise: fn() -> String / <e> |
| 11 | -> String / <e> |
| 12 | { |
| 13 | if when |
| 14 | { |
| 15 | value |
| 16 | } |
| 17 | else |
| 18 | { |
| 19 | otherwise () |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | fn show_pair :: a: Int ~b: String ~c: String -> String |
| 24 | { |
| 25 | "{a}:{b}:{c}" |
| 26 | } |
| 27 | |
| 28 | pub fn main :: -> () / <io> |
| 29 | { |
| 30 | // The callback fills `otherwise`, the unlabelled parameter left. |
| 31 | let result = |
| 32 | { |
| 33 | use <- guard ~when: False ~value: "taken" |
| 34 | "from the callback" |
| 35 | } |
| 36 | io.println result |
| 37 | let taken = |
| 38 | { |
| 39 | use <- guard ~when: True ~value: "taken" |
| 40 | "from the callback" |
| 41 | } |
| 42 | io.println taken |
| 43 | // Mixed calls, both orders. |
| 44 | io.println (show_pair 1 ~c: "see" ~b: "bee") |
| 45 | io.println (show_pair ~b: "bee" 1 ~c: "see") |
| 46 | } |
| 47 | //> from the callback |
| 48 | //> taken |
| 49 | //> 1:bee:see |
| 50 | //> 1:bee:see |