3 ms·
> No runtime exceptions, ever. I'm curious how they handle JSON parsing. For example in Scala/Play you define the class you want to parse to, and if the JSON p
by mcintyre1994 8y ago
> No runtime exceptions, ever.
I'm curious how they handle JSON parsing. For example in Scala/Play you define the class you want to parse to, and if the JSON passed in at runtime doesn't match the shape of that class then you get a runtime exception. Obviously it'd be nice if that didn't happen, but I can't think of any alternative that would make sense.
- pure-awesome 8y agoI could be wrong about this, but I think that, like Elm, it's not technically Grain that throws the runtime exception. It's whatever is reading in the JSON. In more detail: It's purely functional, so it cannot handle I/O at all on its own. It needs some sort of harness into which it fits which handles that, and which then calls the pure functions in Elm / Grain. In Elm's case that harness is JavaScript, and it seems to be a similar case for Grain. (Maybe for Grain it's WebAssembly? I don't honestly know enough about Web Dev to say). During I/O (including user input, and sending data over a network), runtime exceptions can still be thrown, but once it's been converted and passed into the functional part, it's in a sense correctly formatted by definition, and the purely functional Elm / Grain parts don't throw runtime exceptions at all.
- diaz 8y agoYou should look into other more saner json libraries. It's unacceptable to have a runtime error. Nowadays I recommend circe.
- masklinn 8y ago> Obviously it'd be nice if that didn't happen, but I can't think of any alternative that would make sense. Returning a sum of success + error, as e.g. Rust's Serde[0] or Elm's Json.Decode[1] do. The developer gets the feedback that the operation has failed at runtime, but they also get the feedback that the operation can fail and they must handle it somehow at compile-time, even ignoring the issue is an explicit decision. [0] https://docs.serde.rs/serde_json/de/fn.from_str.html https://docs.serde.rs/serde_json/de/fn.from_str.html [1] http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Decode#decodeString http://package.elm-lang.org/packages/elm-lang/core/latest/Js...
- bad_user 8y agoIn Scala too well grown libraries return errors via sum types, e.g. Either. In Play this is actually something like JsSuccess/JsError, not an exception, although Play is pragmatic (aka dirty) at times, so it's possible that it has functions throwing exceptions.