Skip to content

Commit 4dfe84f

Browse files
committed
[ci skip] add docs
1 parent 068fc53 commit 4dfe84f

File tree

5 files changed

+47
-32
lines changed

5 files changed

+47
-32
lines changed

docs/Manual.html

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,7 +3504,7 @@ <h3 id="_return_value_checking_since_1_5_1"><a class="anchor" href="#_return_val
35043504
<pre class="pygments highlight"><code data-lang="ocaml"><span class="tok-k">type</span> <span class="tok-n">element</span>
35053505
<span class="tok-k">type</span> <span class="tok-n">dom</span>
35063506
<span class="tok-k">external</span> <span class="tok-n">getElementById</span> <span class="tok-o">:</span> <span class="tok-kt">string</span> <span class="tok-o">-&gt;</span> <span class="tok-n">element</span> <span class="tok-n">option</span> <span class="tok-o">=</span> <span class="tok-s2">&quot;&quot;</span>
3507-
<span class="tok-o">[@@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-n">send</span><span class="tok-o">.</span><span class="tok-n">pipe</span><span class="tok-o">:</span><span class="tok-n">dom</span><span class="tok-o">]</span> <span class="tok-o">[@@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-n">return</span> <span class="tok-n">null_to_opt</span><span class="tok-o">]</span> <b class="conum">(1)</b>
3507+
<span class="tok-o">[@@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-n">send</span><span class="tok-o">.</span><span class="tok-n">pipe</span><span class="tok-o">:</span><span class="tok-n">dom</span><span class="tok-o">]</span> <span class="tok-o">[@@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-n">return</span> <span class="tok-n">nullable</span><span class="tok-o">]</span> <b class="conum">(1)</b>
35083508

35093509
<span class="tok-k">let</span> <span class="tok-n">test</span> <span class="tok-n">dom</span> <span class="tok-o">=</span>
35103510
<span class="tok-k">let</span> <span class="tok-n">elem</span> <span class="tok-o">=</span> <span class="tok-n">dom</span> <span class="tok-o">|&gt;</span> <span class="tok-n">getElementById</span> <span class="tok-s2">&quot;haha&quot;</span> <span class="tok-k">in</span>
@@ -3516,7 +3516,7 @@ <h3 id="_return_value_checking_since_1_5_1"><a class="anchor" href="#_return_val
35163516
<div class="colist arabic">
35173517
<ol>
35183518
<li>
3519-
<p><code>null_to_opt</code> attribute will automatically convert null to <code>option</code></p>
3519+
<p><code>nullable</code> attribute will automatically convert null and undefined to <code>option</code></p>
35203520
</li>
35213521
</ol>
35223522
</div>
@@ -3525,26 +3525,18 @@ <h3 id="_return_value_checking_since_1_5_1"><a class="anchor" href="#_return_val
35253525
<div class="content">
35263526
<pre class="pygments highlight"><code data-lang="js"><span class="tok-kd">function</span> <span class="tok-nx">test</span><span class="tok-p">(</span><span class="tok-nx">dom</span><span class="tok-p">)</span> <span class="tok-p">{</span>
35273527
<span class="tok-kd">var</span> <span class="tok-nx">elem</span> <span class="tok-o">=</span> <span class="tok-nx">dom</span><span class="tok-p">.</span><span class="tok-nx">getElementById</span><span class="tok-p">(</span><span class="tok-s2">&quot;haha&quot;</span><span class="tok-p">);</span>
3528-
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-nx">elem</span> <span class="tok-o">!==</span> <span class="tok-kc">null</span><span class="tok-p">)</span> <span class="tok-p">{</span> <b class="conum">(1)</b>
3528+
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-nx">elem</span> <span class="tok-o">==</span> <span class="tok-kc">null</span><span class="tok-p">)</span> <span class="tok-p">{</span>
3529+
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
3530+
<span class="tok-p">}</span> <span class="tok-k">else</span> <span class="tok-p">{</span>
35293531
<span class="tok-nx">console</span><span class="tok-p">.</span><span class="tok-nx">log</span><span class="tok-p">(</span><span class="tok-nx">elem</span><span class="tok-p">);</span>
35303532
<span class="tok-k">return</span> <span class="tok-mi">2</span><span class="tok-p">;</span>
35313533
<span class="tok-p">}</span>
3532-
<span class="tok-k">else</span> <span class="tok-p">{</span>
3533-
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
3534-
<span class="tok-p">}</span>
35353534
<span class="tok-p">}</span></code></pre>
35363535
</div>
35373536
</div>
3538-
<div class="colist arabic">
3539-
<ol>
3540-
<li>
3541-
<p>nullable checking without boxing due to compiler optimizations</p>
3542-
</li>
3543-
</ol>
3544-
</div>
35453537
<div class="paragraph">
3546-
<p>Currently 4 directives are supported: <code>null_to_opt</code>, <code>undefined_to_opt</code>,
3547-
<code>null_undefined_to_opt</code> and <code>identity</code>.</p>
3538+
<p>Currently 3 directives are supported: <code>null_to_opt</code>, <code>undefined_to_opt</code>,
3539+
<code>nullable</code> and <code>identity</code>.</p>
35483540
</div>
35493541
<div class="admonitionblock note">
35503542
<table>
@@ -3554,7 +3546,7 @@ <h3 id="_return_value_checking_since_1_5_1"><a class="anchor" href="#_return_val
35543546
</td>
35553547
<td class="content">
35563548
<div class="paragraph">
3557-
<p><code>null_to_opt</code>, <code>undefined_to_opt</code> and <code>null_undefined_to_opt</code> will <strong>semantically</strong>
3549+
<p><code>null_to_opt</code>, <code>undefined_to_opt</code> and <code>nullable</code> will <strong>semantically</strong>
35583550
convert a nullable value to <code>option</code> which is a boxed value, but the compiler will
35593551
do smart optimizations to <strong>remove such boxing overhead</strong> when the returned value is destructed
35603552
in the same routine.</p>

jscomp/test/js_nullable_test.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,34 @@ function eq(loc, x, y) {
2424
return /* () */0;
2525
}
2626

27+
function test(dom) {
28+
var elem = dom.getElementById("haha");
29+
if (elem == null) {
30+
return 1;
31+
} else {
32+
console.log(elem);
33+
return 2;
34+
}
35+
}
36+
2737
function f(x, y) {
2838
console.log("no inline");
2939
return x + y | 0;
3040
}
3141

32-
eq("File \"js_nullable_test.ml\", line 13, characters 7-14", /* false */0, /* false */0);
42+
eq("File \"js_nullable_test.ml\", line 26, characters 7-14", /* false */0, /* false */0);
3343

34-
eq("File \"js_nullable_test.ml\", line 15, characters 7-14", +(f(1, 2) == null), /* false */0);
44+
eq("File \"js_nullable_test.ml\", line 28, characters 7-14", +(f(1, 2) == null), /* false */0);
3545

36-
eq("File \"js_nullable_test.ml\", line 17, characters 6-13", +((null) == null), /* true */1);
46+
eq("File \"js_nullable_test.ml\", line 30, characters 6-13", +((null) == null), /* true */1);
3747

38-
eq("File \"js_nullable_test.ml\", line 21, characters 3-10", /* false */0, /* false */0);
48+
eq("File \"js_nullable_test.ml\", line 34, characters 3-10", /* false */0, /* false */0);
3949

4050
Mt.from_pair_suites("js_nullable_test.ml", suites[0]);
4151

4252
exports.suites = suites;
4353
exports.test_id = test_id;
4454
exports.eq = eq;
55+
exports.test = test;
4556
exports.f = f;
4657
/* Not a pure module */

jscomp/test/js_nullable_test.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ let eq loc x y =
66
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites
77

88

9+
10+
type element
11+
type dom
12+
external getElementById : string -> element option = ""
13+
[@@bs.send.pipe:dom] [@@bs.return nullable]
14+
15+
let test dom =
16+
let elem = dom |> getElementById "haha" in
17+
match elem with
18+
| None -> 1
19+
| Some ui -> Js.log ui ; 2
20+
21+
922
let f x y =
1023
Js.log "no inline";
1124
Js.Nullable.return (x + y)

lib/js/js_null_undefined.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33

44
function bind(x, f) {
5-
if ((x == null)) {
5+
if (x == null) {
66
return x;
77
} else {
88
return f(x);
99
}
1010
}
1111

1212
function iter(x, f) {
13-
if ((x == null)) {
13+
if (x == null) {
1414
return /* () */0;
1515
} else {
1616
return f(x);

site/docsource/OCaml-call-JS.adoc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,38 +1525,37 @@ Below is a contrived core example:
15251525
type element
15261526
type dom
15271527
external getElementById : string -> element option = ""
1528-
[@@bs.send.pipe:dom] [@@bs.return null_to_opt] // <1>
1528+
[@@bs.send.pipe:dom] [@@bs.return nullable] // <1>
15291529
15301530
let test dom =
15311531
let elem = dom |> getElementById "haha" in
15321532
match elem with
15331533
| None -> 1
15341534
| Some ui -> Js.log ui ; 2
15351535
-------------
1536-
<1> `null_to_opt` attribute will automatically convert null to `option`
1536+
<1> `nullable` attribute will automatically convert null and undefined to `option`
15371537
15381538
.Output
15391539
[source,js]
15401540
-------------
15411541
function test(dom) {
15421542
var elem = dom.getElementById("haha");
1543-
if (elem !== null) { // <1>
1543+
if (elem == null) {
1544+
return 1;
1545+
} else {
15441546
console.log(elem);
15451547
return 2;
15461548
}
1547-
else {
1548-
return 1;
1549-
}
15501549
}
15511550
-------------
1552-
<1> nullable checking without boxing due to compiler optimizations
15531551
1554-
Currently 4 directives are supported: `null_to_opt`, `undefined_to_opt`,
1555-
`null_undefined_to_opt` and `identity`.
1552+
1553+
Currently 3 directives are supported: `null_to_opt`, `undefined_to_opt`,
1554+
`nullable` and `identity`.
15561555
15571556
[NOTE]
15581557
======
1559-
`null_to_opt`, `undefined_to_opt` and `null_undefined_to_opt` will *semantically*
1558+
`null_to_opt`, `undefined_to_opt` and `nullable` will *semantically*
15601559
convert a nullable value to `option` which is a boxed value, but the compiler will
15611560
do smart optimizations to *remove such boxing overhead* when the returned value is destructed
15621561
in the same routine.

0 commit comments

Comments
 (0)