darklang / tablecloth

A standard library with the same API in F#, Rescript and OCaml

Home Page:https://www.tablecloth.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Or_error

ethanabrooks opened this issue · comments

I have a project that used Janestreet's Or_error. I would like to add that functionality to tablecloth. If there are objections we could probably fall back to Result.

This is my first time contributing to tablecloth. I'm also fairly new to the dev-ops side of OCaml so I may need a little guidance here and there.

Thanks! What's the reason for having both a Result and an Or_error? I remember reading a Jane St blog post about this, but I didn't really get the reasoning.

I think the best two alternatives are

  1. ('a, string) Result. This is probably what we would go with if Or_error is not an option, but once you put your error in a string, you basically take the error out of the type system, so your error handling has to remain pretty simple. For example, you would not be able to nicely handle situations where an error arises while handling another error (the information from the first error will be lost).

  2. ('a, exn) Result. I don't pretend to fully understand the Janestreet logic for not doing this. I am somewhat new to OCaml and I haven't worked with sexps very much. It sounds like this might work fine for smaller programs but runs into problems in the long run. If nothing else, it's a little verbose and unreadable.

It also sounds like Janestreet wants to a establish a new standard for error handling, and I am kind of pressing the "I believe button" here.

It sounds like both approaches have some pros and some cons, so I think it would be better to stick to the simpler option (option 1), as the OrError approach isn't a common pattern in Rescript or F# (which is coming soon), or indeed outside the Jane St world AFAICT.

Thanks for the suggestions and explanation!

Would you accept a pull request that adds try/catch to your Result module? Or something mirroring Rescript's Exception module?

Yes, I think try/catch would be interesting. Can you post the signatures you're thinking of?

Hmm it seems like Tablecloth is emulating the conventions of the Rescript community. Looking through the Rescript Belt.Result API and this blogpost it looks like the idiomatic way to handle exceptions is with the built-in try .... catch ... syntax. Does that sound right to you?

I'm not really following what you mean? the blogpost doesn't talk about Result at all

Janestreet uses Result.try_with and Or_error.try_with but that doesn't seem to be the approach favored in the Rescript community.

Try_with seems like a good addition, thanks!