News for hackers. Stuff that matters.
Re:Grain: A strongly-typed functional programming language f... by mcintyre1994 on Tuesday July 31, 2018 @04:54AM (#17651590)
> 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.

Re:Grain: A strongly-typed functional programming language f... by masklinn on Tuesday July 31, 2018 @05:54AM (#17651831)
> 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

[1] http://package.elm-lang.org/packages/elm-lang/core/latest/Js...

Re:Grain: A strongly-typed functional programming language f... by bad_user on Tuesday July 31, 2018 @08:41AM (#17652661)
In 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.

Re:Grain: A strongly-typed functional programming language f... by diaz on Tuesday July 31, 2018 @05:22AM (#17651708)
You should look into other more saner json libraries. It's unacceptable to have a runtime error. Nowadays I recommend circe.
Re:Grain: A strongly-typed functional programming language f... by pure-awesome on Tuesday July 31, 2018 @05:20AM (#17651695)
I 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.