some messy unsafePerformIO usage

some messy unsafePerformIO usage

I wanted to parse some data using Aeson, with the caveat that additional, uninspected keys were not welcome and should result in an error being thrown.

Aeson doesn't seem to come with a mechanism to do this, so I made my own. I add new lookup operators that register the requested key, so that later on I can detect if there are more keys in the object than were inspected.

I didn't really want the hassle of a monad transformer (add a huge amount of lifts to the parsing code), so I used an IORef. When I compiled the code, I ran into the issue that usafePerformIO $ newIORef ... would be cached, so only one IORef would ever get created. So I had to manually sequence pure and impure operations, and for this i created unsafeDepend.

This is… Horrible, right?

Is there a nicer way, that doesn't involve a monad transformer ie StateT (Set Text) Parser a?

Submitted July 16, 2017 at 07:30PM by libscott
via reddit


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s