Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Proposing new syntax for zero-cost unwrapping options/results

$
0
0

I played with this last week.

Some feedback:

  • This should also work for Null.t and Nullable.t.
  • It would be more ergonomic if we could mix various types. For example:
let updateH2Color2 = () => {
  let? Some(element) = dollar("h2")
  let? Ok(h2) = element->tryMapHTMLHeadingElement
  h2.style.color = "blue"
  Some()
}

could maybe become

let updateH2Color2 = () => {
  @let.unwrap
  switch dollar("h2") {
  | None => Error(xyz) // not sure yet how to define xyz
  | Some(element) =>
    @let.unwrap
    switch tryMapHTMLHeadingElement(element) {
    | Error(_)  as x => x
    | Ok(h2) =>
      h2.style.color = "blue"
      Some()
    }
  }
}

I get that xyz needs to come from somewhere, so that is challenging, I agree. But mixing option, null and nullable should be more straightforward. You could return the empty case of the last let?, I think.

In my examples, I was also grabbing data to finally mutate something. The last meaningful expression was h2.style.color = "blue". Having to return result<unit, _> didn’t add that much value. Again, this depends on the situation. This could have been:

let updateH2Color2 = () => {
  @let.unwrap
  switch dollar("h2") {
  | None => ()
  | Some(element) =>
    @let.unwrap
    switch tryMapHTMLHeadingElement(element) {
    | Error(_)  => ()
    | Ok(h2) =>
      h2.style.color = "blue"
    }
  }
}

Of course, I don’t know how easy these suggestions are to implement.


Viewing all articles
Browse latest Browse all 2592

Trending Articles