I’m wrapping a C library in Haskell right now and am having trouble in the following scenario:
- The library offers the ability register “named properties”. You register these before giving up control to the library completely (i.e. the library runs your main loop).
- The library loads the properties’ values from a database and you can query them in a callback.
In other words, this is the Haskell pseudocode for my wrapper:
main = do
library_init ["property1", "property2"] afterInit
library_start
afterInit ptr = do
property1Value <- read_value ptr "property1"
putStrLn property1Value
Hopefully, the problem is clear: With this raw a wrapper, you can call read_value
on a property you haven’t specified in library_init
, resulting in a run-time error.
Is there a Haskell-idiomatic way to make this safer? I was thinking about something like a “property parser” thingy, as in:
data MyProperties = MyProperties String String
readProps = MyProperties <$> readProp "property1" <*> readProp "property2"
And then somehow extracting “property1” and “property2” from this “applicatified” parser, passing that to library_init
(and its values afterInit
). But I’m not sure how to construct this and if it’s wise to do so.
Is there any prior work here, or any pointers?