tobogganSe connecter

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.

72 lignes · 1379 octetsBrut · Historique
1pub type User { User(name: String, age: Int) }
2
3pub type Shape { Circle(r: Float), Square(side: Float) }
4
5pub fn name :: u: User -> String
6{
7 u.name
8}
9
10pub fn birthday :: u: User -> User
11{
12 User(..u, age: u.age + 1)
13}
14
15pub fn make :: -> User
16{
17 User(age: 3, name: "Ada")
18}
19
20pub fn bad_label :: -> User
21{
22 User(name: "x", years: 3) //~ ERROR E0204 no parameter or field named `years`
23}
24
25pub fn missing_field :: -> User
26{
27 User("x") //~ ERROR E0205
28}
29
30pub fn no_such_field :: u: User -> Int
31{
32 u.height //~ ERROR E0206 no field named `height`
33}
34
35pub fn several_ctors :: s: Shape -> Float
36{
37 s.r //~ ERROR E0206 several constructors
38}
39
40pub fn area :: s: Shape -> Float
41{
42 match s { Circle(r) -> 3.14 * r * r, Square(side: x) -> x * x }
43}
44
45pub fn pattern_arity :: u: User -> String
46{
47 match u
48 { User(n) -> n //~ ERROR E0205
49 }
50}
51
52pub 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.
58fn apply :: u: User f: fn(User) -> String -> String
59{
60 f u
61}
62pub 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).
69pub fn resize :: s: Shape -> Shape
70{
71 Circle(..s, r: 1.0) //~ ERROR E0204 `Shape` has several constructors
72}