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

Rescript frustration

$
0
0

You’re right, but if a switch case doesn’t return the same type, you can then change its type in the branch as you did in your code actually, something you cannot do by just using ?:

| None => Error(UnrecognizedArg(`Unrecognized argument: '${name}'`))

You could also have something a bit more flexible by using polymorphic variants in the error type, then you could indeed just use an equivalent of ? with that.

Regarding Core, I think it’d be better to check the official documentation because Core got merged into the main repo and the development is done there, so you’d rather check there for the latest APIs or the docs once they’re synced (v12 is not published yet for example).

But you’re right, there’s no helper functions that expect thunks, but it should not be a big deal adding them, we’d definitely welcome a PR for this!

Thanks for sharing your code, I think there are mainly two cases, the one where you do interact with your error cases, then you do want to avoid “swallowing” errors and using early returns is not a good idea then, and the second case where you just want to short-circuit errors and in this case I think using exceptions is a good match then:

let parts = str->split
let result = parts->Array.reduce(dict{}, (acc, name) => {
  let arg =
    args
    ->Dict.get(name)
    ->OptionExt.getCustomExn(
      UnrecognizedArg(`Unrecognized argument: '${name}'`),
    )
  let value = handleArg(arg, parts)
  switch acc->Dict.get(name) {
  | Some(_) =>
    raise(TooManyOccurrences(`Argument '${name}' must only be specified once`))
  | None => {
      acc->Dict.set(name, value)
      acc
    }
  }
})

Playground link


Viewing all articles
Browse latest Browse all 1751

Trending Articles