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.

http://ift.tt/2txhdUg

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 http://ift.tt/2t7SPJz

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s