forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_page.ml
71 lines (66 loc) · 2.04 KB
/
demo_page.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
let rec fib = function
| 1 | 2 -> 1
| n -> fib (n - 1 ) + fib (n - 2)
(** Imperative style *)
let sum n =
let v = ref 0 in
for i = 0 to n do
v := !v + i
done;
!v
(** List map *)
type 'a list =
| Nil
| Cons of 'a * 'a list
let rec map f = function
| Nil -> Nil
| Cons (x,xs) -> Cons (f x, map f xs)
(** Test curry and uncurry calling convention *)
let test_curry x y = x + y
let f = test_curry 32
(** Create a typed binding for react *)
type t
type element
external document : unit -> t = "" [@@js.global "document"] [@@js.scope "window"]
external getElementById : t -> string -> element = "" [@@js.send "getElementById"]
(** Phantom types *)
type config
type component
type attrs
type component_class
external config :
?display_name:string ->
render:(unit -> component) -> unit ->
config = "" [@@js.obj ] (** make a json object *)
external attrs:
?alt: string ->
?autoPlay: bool ->
unit -> attrs = "" [@@js.obj]
external str : string -> component = "%identity"
external h1 : ?attrs:attrs -> component array -> component = ""
[@@js.call "h1"] [@@js.scope "DOM"] [@@js.module "react"] [@@js.splice] (** splice the last argument *)
external h2 : ?attrs:attrs -> component array -> component = ""
[@@js.call "h2"] [@@js.scope "DOM"] [@@js.module "react"] [@@js.splice]
external div : ?attrs:attrs -> component array -> component = ""
[@@js.call "div"] [@@js.scope "DOM"] [@@js.module "react"] [@@js.splice]
external createClass :
config -> component_class = "createClass"
[@@js.call "createClass"]
[@@js.module "react"]
external render : component_class -> element -> unit = ""
[@@js.call "render"]
[@@js.module "react-dom"]
;;
(** Do the rendering *)
render (
createClass (
(config
~render:(fun _ ->
div
~attrs:(attrs ~alt:"pic" ())
[|
h1 [| str "hello react"|];
h2 [| str "type safe!" |];
|]
)
()))) (getElementById (document ()) "hi")