I am trying to use the "when" function in my program because I want to have an action preformed if something is true and not if its not true. I could also accomplish the same thing by using the Maybe monad but that will clutter the rest of my program up.
The problem is that when expects the return type to be IO() but I want to make it (or my own version) work for IO(Response ByteString)
This is my function code so far:
mayNotifyDs :: Bool -> String -> ByteString -> IO (Response ByteString)
mayNotifyDs hasRel specName details =
when hasRel (post addr ("dummy" := details))
where addr = "http://127.0.0.1:8082/dummydir/" ++ specName
edit: A "print" snuck into the code I originally posted, this was something I experimented with and I have since removed it. I can use print but then I need to also use unsafeLocalState from foreign.marshall, and that's not advisable is it? The code would in this case become the same but with when hasRel (print $ unsafeLocalState (post addr ("dummy" := details)))
I settled on a solution here. I ended up using unsafeLocalState after all seeing as I'm already checking if said value exists, originally with bool but eventually with the maybe monad.
edit: please note I do not want to cast an error here; both cases should print something for logging purposes.
edit2: changing my answer to be in line with what Lee Jan mentioned here, seeing as its a better solution.
mayNotifyDs :: String -> Maybe ByteString -> IO ()
mayNotifyDs specName details = case details of
(Just _) -> post addr ("dummy" := details) >>= print
Nothing -> putStrLn "Message about fail"
where addr = "http://127.0.0.1:8082/dummydir/" ++ specName