が起こっている私は、クラウドHaskellのパッケージのEncoding.hsを見ている、と私は、誰かが私をよりよく理解するのに役立つ可能性が期待していたいくつかの奇妙なコードに遭遇しています。必要なコードが含まれています:Haskellの魔法のコード、何がここ
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
私はちょうど$! (私はちょうど厳密に評価していると推測しています)、なぜidトリックが必要なのでしょうか?また、私は特にこのラインに問題が生じています:
if (decode $! payloadType a) == show (typeOf $ id undefined)
私はこれを推測している、payloadTypeが何らかの理由で無効である場合は見ているが、そのような場合には、その後、他の句を切り替えるべきではありませんすなわち変更:あなたが提供することができます任意の助け
if (decode $! payloadType a) == show (typeOf $ id undefined)
then Just (id $! decode pc)
else Nothing
if (decode $! payloadType a) == show (typeOf $ id undefined)
then Nothing
else Just (id $! decode pc)
に感謝します。
はい、 'f $! xは厳格なアプリケーションです。厳密性の追加方法について[この記事(http://neilmitchell.blogspot.com/2008/05/bad-strictness.html)]が啓発されているかもしれません。 –
も参照してくださいhttp://stackoverflow.com/q/2787543/246886 –