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 | // Inside a GADT arm, inference decides fields and HTML holes from the |
| 2 | // refined type; Core follows those decisions, since the stored type is the |
| 3 | // arm's unrefined rigid (issue #235). |
| 4 | |
| 5 | import std/html.{type Html} |
| 6 | import std/io |
| 7 | |
| 8 | type Point { Point(x: Int, y: Int, label: String) } |
| 9 | |
| 10 | type Is<a> |
| 11 | { IsPoint -> Is<Point> |
| 12 | , IsInt -> Is<Int> |
| 13 | , IsBool -> Is<Bool> |
| 14 | , IsString -> Is<String> |
| 15 | , IsList -> Is<List<Html>> |
| 16 | } |
| 17 | |
| 18 | fn second :: w: Is<a> v: a -> Int |
| 19 | { |
| 20 | match w { IsPoint -> v.y, _ -> 0 } |
| 21 | } |
| 22 | |
| 23 | fn label :: w: Is<a> v: a -> String |
| 24 | { |
| 25 | match w { IsPoint -> v.label, _ -> "" } |
| 26 | } |
| 27 | |
| 28 | fn attr :: w: Is<a> v: a -> Html |
| 29 | { |
| 30 | match w |
| 31 | { IsInt -> <p n={v}>{v}</p> |
| 32 | , IsBool -> <p hidden={v}></p> |
| 33 | , IsString -> <p title={v}>{v}</p> |
| 34 | , IsList -> <ul>{v}</ul> |
| 35 | , IsPoint -> <p>{v.x}</p> |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn main :: -> () / <io> |
| 40 | { |
| 41 | io.println (show (second IsPoint Point(1, 2, "p"))) |
| 42 | io.println (label IsPoint Point(1, 2, "p")) |
| 43 | io.println (html.render (attr IsInt 42)) |
| 44 | io.println (html.render (attr IsBool True)) |
| 45 | io.println (html.render (attr IsString "a<b")) |
| 46 | io.println (html.render (attr IsList [<li>one</li>, <li>two</li>])) |
| 47 | io.println (html.render (attr IsPoint Point(7, 8, "q"))) |
| 48 | } |
| 49 | //> 2 |
| 50 | //> p |
| 51 | //> <p n="42">42</p> |
| 52 | //> <p hidden></p> |
| 53 | //> <p title="a<b">a<b</p> |
| 54 | //> <ul><li>one</li><li>two</li></ul> |
| 55 | //> <p>7</p> |