Force show a record in PureScript

落爺英雄遲暮 提交于 2019-12-10 15:36:33

问题


Is it possible to force-show (i.e., create a string representation) an arbitrary record in PureScript for debugging purpose regardless of it having a type-class instance for Show?

I would like to show the contents of the Pux Event object but it does not have a Show instance:

  No type class instance was found for

    Data.Show.Show { target :: { value :: String
                               , checked :: Boolean
                               }
                   , currentTarget :: { value :: String
                                      , checked :: Boolean
                                      }
                   , altKey :: Boolean
                   , button :: Number
                   , buttons :: Number
                   , clientX :: Number
                   , clientY :: Number
                   , ctrlKey :: Boolean
                   , metaKey :: Boolean
                   , pageX :: Number
                   , pageY :: Number
                   , screenX :: Number
                   , screenY :: Number
                   , shiftKey :: Boolean
                   }

回答1:


You can use purescript-debug.




回答2:


You can wrap a record in a newtype and use Data.Generic to derive the instance for it:

import Data.Generic

newtype MyRecord = MyRecord
                   { target :: { value :: String
                               , checked :: Boolean
                               }
                   , currentTarget :: { value :: String
                                      , checked :: Boolean
                                      }
                   , altKey :: Boolean
                   , button :: Number
                   , buttons :: Number
                   , clientX :: Number
                   , clientY :: Number
                   , ctrlKey :: Boolean
                   , metaKey :: Boolean
                   , pageX :: Number
                   , pageY :: Number
                   , screenX :: Number
                   , screenY :: Number
                   , shiftKey :: Boolean
                   }


derive instance genericMyRecord :: Generic MyRecord

instance showMyRecord :: Show MyRecord where
    show = gShow

Now you can use show on MyRecord or show <<< MyRecord on the record.



来源:https://stackoverflow.com/questions/40530747/force-show-a-record-in-purescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!