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

Rescript frustration

$
0
0

i’m having trouble understanding this take. this happens to be part of the cli, but it could be anything else, let’s say network stream packet parsing where you want to be able to resume anyway.

i think the “carrying results around” way-of-thinking here stems from the fact there you’re forced to carry them around in rescript.

my prior code code have been something like:

  let result = switch str->split {
  | Ok(parts) => {
      parts->Array.reverse

      let rec processArgs = (acc) => {
        let Some(name) = argv->Array.pop else return Error(NoMoreArgs)
        let Some(arg) = args->Dict.get(name) else return Error(UnrecognizedArg(`Unrecognized argument: '${name}'`)
        switch processArg(arg, argv) {
        | Ok(value) =>  {
          acc->Dict.set(name, value)
        }
        |  Error(e) => Error(e)
        }
      }

      processArgs(Dict.make())
    }

this kind of early return sanity checks, or guards, make life so, so much easier, and you don’t end up with either a huge, hard-to-read switch statement, or ridiculous nesting.


Viewing all articles
Browse latest Browse all 2592

Trending Articles