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

Rescript: How to model package.json / collection of two strings

$
0
0

In ReScript, Array access returns an option by default instead of giving you a runtime error when the Array is empty.

let deps = switch result.scripts[0] {
  | Some(packageData) => packageData.name
  | None => "(empty)"
}

Console.log("deps: " ++ deps)

It can be solved a bit more concisely with a helper function from the built-in Option module.

Console.log(
  "deps: " ++
  result.scripts[0]->Option.mapOr("(empty)", packageData => packageData.name),
)

Or, if you want the plain JS behavior for some reason, you can use Array.getUnsafe. Only use that when you know for a fact the array is populated.

Console.log("deps: " ++ (result.scripts->Array.getUnsafe(0)).name)

Viewing all articles
Browse latest Browse all 2592

Trending Articles