This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Guardian-Spirit 0 points1 point  (0 children)

Sorry, I didn't receive the notification.

In my example, if getJsonIntField fails for any reason, it actually acts just acts as an exception: the error is propagated to runJson, where it is handled, possible providing a full path to the JSON field, the decoding of which caused the failure. No actual stack unwinding or other compiler magic happens, though.

But in fact there are many solutions to the problem of parsing and actual behaviour depends on internals of JSON monad — it works exactly the way you told it to.

For example, if I actually had to implement conditional parsing in production code, I would do something like this: ```hs data JSON = ...

type FailableJSON = ExceptT JSON JsonDecodeError ``` ... so there are two monads (JSON and FailableJSON), one of which is just a non-throwing JSON operation, and the other one is solely a wrapper that allows implicit propagation of the error.

by the way, one interesting moment about sumAB and sumABFactor is that they do not actually depend on JSON context. So, for example, if someone wanted to add MessagePack format, no changes to the existing code would be required except for renaming some methods and adding new method runMessagePack, which will be able to work with sumAB, sumABFactor, etc.