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 | pub type User { User(name: String, age: Int) } |
| 2 | |
| 3 | pub type Shape { Circle(r: Float), Square(side: Float) } |
| 4 | |
| 5 | pub fn name :: u: User -> String |
| 6 | { |
| 7 | u.name |
| 8 | } |
| 9 | |
| 10 | pub fn birthday :: u: User -> User |
| 11 | { |
| 12 | User(..u, age: u.age + 1) |
| 13 | } |
| 14 | |
| 15 | pub fn make :: -> User |
| 16 | { |
| 17 | User(age: 3, name: "Ada") |
| 18 | } |
| 19 | |
| 20 | pub fn bad_label :: -> User |
| 21 | { |
| 22 | User(name: "x", years: 3) //~ ERROR E0204 no parameter or field named `years` |
| 23 | } |
| 24 | |
| 25 | pub fn missing_field :: -> User |
| 26 | { |
| 27 | User("x") //~ ERROR E0205 |
| 28 | } |
| 29 | |
| 30 | pub fn no_such_field :: u: User -> Int |
| 31 | { |
| 32 | u.height //~ ERROR E0206 no field named `height` |
| 33 | } |
| 34 | |
| 35 | pub fn several_ctors :: s: Shape -> Float |
| 36 | { |
| 37 | s.r //~ ERROR E0206 several constructors |
| 38 | } |
| 39 | |
| 40 | pub fn area :: s: Shape -> Float |
| 41 | { |
| 42 | match s { Circle(r) -> 3.14 * r * r, Square(side: x) -> x * x } |
| 43 | } |
| 44 | |
| 45 | pub fn pattern_arity :: u: User -> String |
| 46 | { |
| 47 | match u |
| 48 | { User(n) -> n //~ ERROR E0205 |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | pub fn pattern_rest :: u: User -> String |
| 53 | { |
| 54 | match u { User(name: n, ..) -> n } |
| 55 | } |
| 56 | |
| 57 | // Callback parameters get their type from the expected function type. |
| 58 | fn apply :: u: User f: fn(User) -> String -> String |
| 59 | { |
| 60 | f u |
| 61 | } |
| 62 | pub fn callback :: u: User -> String |
| 63 | { |
| 64 | apply u fn x { x.name } |
| 65 | } |
| 66 | |
| 67 | // Record update copies the other fields by position, so the base must |
| 68 | // have the constructor: a type with several can't be updated (issue #150). |
| 69 | pub fn resize :: s: Shape -> Shape |
| 70 | { |
| 71 | Circle(..s, r: 1.0) //~ ERROR E0204 `Shape` has several constructors |
| 72 | } |