How to "wrap" monadic return value

82 views Asked by At

I have the following code:

data APNSIdentifier = NoIdentifier | Identifier Word32 deriving (Show, Eq)
newtype APNSItem = Item Put

createNotificationIdentifierItem :: APNSIdentifier -> APNSItem   <--- here
createNotificationIdentifierItem (Identifier identifier) = do
    putWord8 3
    putWord16be 4
    putWord32be identifier

How can I "wrap" the Put monad with an APNSItem? Do I have to make APNSItem an instance of the Monad typeclass or is there a simpler solution?

1

There are 1 answers

2
Fraser On BEST ANSWER
createNotificationIdentifierItem :: APNSIdentifier -> APNSItem
createNotificationIdentifierItem (Identifier identifier) = Item $ do
    putWord8 3
    putWord16be 4
    putWord32be identifier

or

createNotificationIdentifierItem :: APNSIdentifier -> APNSItem
createNotificationIdentifierItem (Identifier identifier) = do
    Item $ putWord8 3
    Item $ putWord16be 4
    Item $ putWord32be identifier

after making APNSItem an instance of Monad (you can do this with GeneralizedNewtypeDeriving, but you'll need to fix APNSItem to have a single type variable first)