I'm looking over the Cloud Haskell package's Encoding.hs, and encountered some strange code that I was hoping someone could help me better understand. Included is the necessary code:
class (Binary a,Typeable a) => Serializable a
instance (Binary a,Typeable a) => Serializable a
data Payload = Payload
{
payloadType :: !ByteString,
payloadContent :: !ByteString
} deriving (Typeable)
serialDecodePure :: (Serializable a) => Payload -> Maybe a
serialDecodePure a = (\id ->
let pc = payloadContent a
in pc `seq`
if (decode $! payloadType a) == show (typeOf $ id undefined)
then Just (id $! decode pc)
else Nothing ) id
I'm just curious about what the $! does (I'm guessing just strictly evaluates), and also why we need the id trick (something with lazy evaluation?). Also I am specifically having problems with this line:
if (decode $! payloadType a) == show (typeOf $ id undefined)
I'm guessing this is seeing if the payloadType is invalid for whatever reason, but if that is the case shouldn't the then and else clauses be switched, ie change:
if (decode $! payloadType a) == show (typeOf $ id undefined)
then Just (id $! decode pc)
else Nothing
to
if (decode $! payloadType a) == show (typeOf $ id undefined)
then Nothing
else Just (id $! decode pc)
Thanks for any help you can provide.
f $! x
is strict application. You may find this article enlightening about how adding strictness works. – Wondawonder