Why does `traceId` return the message string itself?

允我心安 提交于 2019-12-25 07:39:53

问题


I found it really weird that traceId returns the message string itself. Why would anyone want to do that?

Is there a common scenario where traceId would come in handy?


回答1:


EDIT:

Credits to David who drew attention on the fact that my traceId' is exactly traceShowId.

Hence, traecId is simply the String -> String monomorphic version of the polymorphic traceShowId :: Show a -> a -> a, which also strips the printed string from quotes.

Old answer:

One way to interpret traceId is that it's a String-specialized version of a more general traceId' which is not in the stdlib:

traceId' :: Show a => a -> a
traceId' x = trace (show x) x

so that

traceId = traceId' :: String -> String

Example:

my = id; func = id; chain = id; value = 3

main = do
  print $ length $ traceId "chain is called!"
  print $ length $ traceId' [1,2,3]

but for some reason the general version of traceId, which I've called traceId', is not in the stdlib.



来源:https://stackoverflow.com/questions/33059378/why-does-traceid-return-the-message-string-itself

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