Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Result module to Belt #2621

Merged
merged 17 commits into from May 17, 2018
Merged

Add Result module to Belt #2621

merged 17 commits into from May 17, 2018

Conversation

mchaver
Copy link
Contributor

@mchaver mchaver commented Mar 13, 2018

Most of these functions are from here
https://gist.github.com/NicolasT/65dad40b203da7c65b4c
I added rights, lefts, arrayLefts and arrayRights.

@mchaver
Copy link
Contributor Author

mchaver commented Mar 13, 2018

It's probably breaking tests because I am not sure of all the places I need to reference Belt_Either, but do you think this looks good?

@andreypopp
Copy link

I'm just starting to use Belt but this looks to me like it breaks few Belt (or OCaml) conventions:

  • Why not type ('a, 'err) result = Ok of 'a | Error of 'err, OCaml has built-in result type since 4.03 (I think) would be nice to support it in Belt instead of rolling own isomorphic type?
  • Methods should be all in camelCase in Belt
  • Not sure if bimap, pure are needed... at least at the start.

@mchaver
Copy link
Contributor Author

mchaver commented Mar 13, 2018

@andreypopp
Yeah, I can rename it to Result and fix the camel case. I would like to have bimap, the monadic stuff liek pure, bind, etc. is less important for me.

@mchaver
Copy link
Contributor Author

mchaver commented Mar 13, 2018

@andreypopp
It's untested but do you like the naming better?

@mchaver mchaver changed the title Add Either to Belt Add Result module to Belt Mar 14, 2018
@chenglou
Copy link
Member

@mchaver let's get this in. Though is it possible to ship a subset of this first? There are quite a but of bikeshedding opportunity that we can avoid if this is just the same subset as e.g. #2622 for now.

@mchaver
Copy link
Contributor Author

mchaver commented Mar 26, 2018

@chenglou sure, I can make it match the same subset of functions as option

@mchaver
Copy link
Contributor Author

mchaver commented Mar 26, 2018

@chenglou What is the proper way to get the tests running locally?
I did yarn and yarn test
It runs some of the tests but it can't find Belt_Option

Building ../others/belt.ml
File "/home/james/work/mchaver-bucklescript/jscomp/others/belt.ml", line 244, characters 16-27:
Error: Unbound module Belt_Option
Makefile:290: recipe for target '../others/belt.cmj' failed
make[1]: *** [../others/belt.cmj] Error 

@chenglou
Copy link
Member

Try cd jscomp/others && make depend && make all

@mchaver
Copy link
Contributor Author

mchaver commented Mar 27, 2018

make depend seemed to work, but then I got this for make all.

../../lib/bsc.exe  -no-alias-deps -bs-no-version-header -absname -bs-diagnose -bs-no-check-div-by-zero -bs-cross-module-opt -bs-noassertfalse -bs-package-name bs-platform  -nostdlib -bs-package-output commonjs:lib/js -bs-package-output es6:lib/es6  -I ../runtime -I ../stdlib -w +3-40-49 -warn-error A -bin-annot  -c belt_SortArrayString.ml
Building belt_SortArrayString.ml
File "/home/james/work/mchaver-bucklescript/jscomp/others/belt_SortArrayString.ml", line 1:
Error: The implementation belt_SortArrayString.ml
       does not match the interface belt_SortArrayString.cmi:
       The value `diff' is required but not provided
       The value `intersect' is required but not provided
       The value `union' is required but not provided
       The value `binarySearch' is required but not provided
       The value `stableSort' is required but not provided
       The value `stableSortInPlace' is required but not provided
       The value `isSorted' is required but not provided
       The value `strictlySortedLength' is required but not provided
       The type `element' is required but not provided
Makefile:178: recipe for target 'belt_SortArrayString.cmj' failed
make: *** [belt_SortArrayString.cmj] Error 2

@chenglou
Copy link
Member

Humm... I suggest you stash the changes, do a git clean -xdf, then re-run the stuff at https://github.com/BuckleScript/bucklescript/blob/master/CONTRIBUTING.md#setup

cc @bobzhang

Copy link
Member

@chenglou chenglou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping bob or me on discord if you can't make the build work. Sorry for the trouble; we're trying to streamline it still

let mapWithDefault opt default f = mapWithDefaultU opt default (fun[@bs] x -> f x)

let mapU opt f = match opt with
| Ok x -> Some (f x [@bs])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok (f x [@bs])

* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)


type ('a, 'b) t =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you like this or should this just point to Js_result.t?

@chenglou
Copy link
Member

chenglou commented Mar 27, 2018

I've fixed your errors inline. Now the build passes!

The interface file is basically like what's from https://github.com/BuckleScript/bucklescript/pull/2622/files. So I think this is good to go. One last question about whether we should alias the type, then that's it. We should redirect folks from Js.Option and Js.Result to here

@mchaver
Copy link
Contributor Author

mchaver commented Mar 27, 2018

@chenglou Thanks for helping me clean it up and showing me how to build it. I switched the type to an alias type ('a,'b) t = ('a, 'b) Js_result.t

@chenglou
Copy link
Member

@bobzhang should we change Js.Result to refer to Belt.Result or vice-versa?

Copy link
Member

@bobzhang bobzhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This in general looks good to me


type ('a,'b) t = ('a, 'b) Js_result.t = Ok of 'a | Error of 'b

val getExn : ('a, 'b) t -> 'a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should getExn contain some information about Error ?

@chenglou
Copy link
Member

chenglou commented Apr 9, 2018

should we change Js.Result to refer to Belt.Result or vice-versa?

I think we should flip it and make Js.Result refer to Belt.Result, then put deprecation notices on Js.Result

@mchaver
Copy link
Contributor Author

mchaver commented Apr 27, 2018

@chenglou
I think I might be missing something. Do dependencies need to be reordered so js_result can find Belt.Result?

File "/usr/local/lib/node_modules/bs-platform/jscomp/others/js_result.mli", line 25, characters 18-40:
Error: Unbound module Belt_Result
Makefile:176: recipe for target 'js_result.cmi' failed
make[2]: *** [js_result.cmi] Error 2
make[2]: *** Waiting for unfinished jobs....
Building belt_SortArrayString.mli
Building js_mapperRt.mli
Building belt_SortArrayInt.mli
Building belt_Id.mli
Building belt_Array.mli
Building belt_List.mli
make[2]: Leaving directory '/usr/local/lib/node_modules/bs-platform/jscomp/others'
Makefile:14: recipe for target 'libs' failed
make[1]: *** [libs] Error 2
make[1]: Leaving directory '/usr/local/lib/node_modules/bs-platform'
Makefile:9: recipe for target 'world' failed
make: *** [world] Error 2
child_process.js:516
    throw err;

@mchaver
Copy link
Contributor Author

mchaver commented May 10, 2018

@chenglou @bobzhang
Could you guys take another look at this?

@bobzhang bobzhang merged commit d8f50d2 into rescript-lang:master May 17, 2018
@chenglou
Copy link
Member

chenglou commented May 18, 2018

@mchaver thanks for the PR! =D And sorry for the back-and-forth, but it looks great now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants