File: camlp4.wiki

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (60 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download | duplicates (3)
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
= Camlp4 syntax extension for Js_of_ocaml

WARNING: The Camlp4 syntax extension is not longer part of the js_of_ocaml distribution.

A Camlp4 syntax extension is available for manipulating object properties,
invoking methods and creating objects.
We advise to use the <<a_manual chapter="ppx" |Ppx>> syntax extension instead.

The syntax and typing rules are as follows:

* Getting a property
{{{
obj : <m : u prop> Js.t
-----------------------
     obj##m : u
}}}

* Setting a property
{{{
obj : <m : u prop> Js.t
  e : u
-----------------------
   obj##m <- e : unit
}}}

* Invoking a method
{{{
obj : <m : t_1 -> ... -> t_n -> u meth; ..> Js.t
    e_i : t_i               (1 <= i <= n)
-------------------------------------------------
          obj##m(e_1, ..., e_n) : u
}}}

* Using an object constructor
{{{
constr : (t_1 -> ... -> t_n -> u Js.t) Js.constr
e_i : t_i               (1 <= i <= n)
------------------------------------------------
        jsnew constr (e1, ..., en) : u Js.t
}}}

* Creating a literal object
<<code language="ocaml"|
jsobject (self) (* Equivalent of this *)
  val x = 3 (* read-only prop *)
  val mutable y = 4 (* read/write prop *)
  method foo i = self##y <- self##x + i
end
>>
Properties are defined with the [val] keyword. [mutable] makes the
property writable. [self] can be any identifier and will be bind
to [this] in javascript.

In this case, the object has the following type:
<<code language="ocaml"|
< foo : int -> unit Js.meth;
    x : int Js.readonly_prop;
    y : int Js.prop
> Js.t
>>