記錄 newtype

記錄語法可以與 newtype 一起使用,限制條件是隻有一個建構函式只有一個欄位。這裡的好處是自動建立一個解包 newtype 的函式。這些欄位通常以 run 為單位開頭,get 為 monoids,un 為其他型別。

newtype State s a = State { runState::s -> (s, a) }

newtype Product a = Product { getProduct::a }

newtype Fancy = Fancy { unfancy::String } 
  -- a fancy string that wants to avoid concatenation with ordinary strings

重要的是要注意,記錄語法通常從不用於形成值,並且欄位名稱嚴格用於展開

getProduct $ mconcat [Product 7, Product 9, Product 12]
-- > 756