Skip to content

Commit a01e0f4

Browse files
authored
Merge pull request #1061 from chenglou/from_opt
Implement from_opt for Js_null, js_undefined, js_null_undefined
2 parents c687bdb + 33d1141 commit a01e0f4

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

jscomp/others/.depend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bs_dyn.cmj : bs_dyn.cmi
1515
bs_dyn_lib.cmj : bs_dyn.cmj bs_dyn_lib.cmi
1616
node_child_process.cmj :
1717
js_boolean.cmj : js_boolean.cmi
18+
js_math.cmj :
1819
js_re.cmi :
1920
js_null_undefined.cmi :
2021
js_types.cmi :

jscomp/others/js_null_undefined.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,7 +17,7 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
@@ -32,8 +32,13 @@ let bind x f =
3232
match to_opt x with
3333
| None -> empty
3434
| Some x -> return (f x [@bs])
35-
35+
3636
let iter x f =
37-
match to_opt x with
37+
match to_opt x with
3838
| None -> ()
3939
| Some x -> f x [@bs]
40+
41+
let from_opt x =
42+
match x with
43+
| None -> empty
44+
| Some x -> return x

jscomp/runtime/js_null.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ external return : 'a -> 'a t = "%identity"
2929
external test : 'a t -> bool = "js_is_nil"
3030
external empty : 'a t = "null" [@@bs.val]
3131
(*end::interface_all[]*)
32+
3233
let bind x f =
33-
match to_opt x with
34-
| None -> empty
34+
match to_opt x with
35+
| None -> empty
3536
| Some x -> return (f x [@bs])
37+
3638
let iter x f =
3739
match to_opt x with
38-
| None -> ()
40+
| None -> ()
3941
| Some x -> f x [@bs]
42+
43+
let from_opt x =
44+
match x with
45+
| None -> empty
46+
| Some x -> return x

jscomp/runtime/js_undefined.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,17 +17,17 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26-
type + 'a t = 'a Js.undefined
26+
type + 'a t = 'a Js.undefined
2727
external to_opt : 'a t -> 'a option = "js_from_def"
2828
external return : 'a -> 'a t = "%identity"
2929
external test : 'a t -> bool = "js_is_undef"
30-
external empty : 'a t = "undefined"
30+
external empty : 'a t = "undefined"
3131
[@@bs.val]
3232

3333
let bind x f =
@@ -37,5 +37,10 @@ let bind x f =
3737

3838
let iter x f =
3939
match to_opt x with
40-
| None -> ()
40+
| None -> ()
4141
| Some x -> f x [@bs]
42+
43+
let from_opt x =
44+
match x with
45+
| None -> empty
46+
| Some x -> return x

0 commit comments

Comments
 (0)