簡體   English   中英

Haskell:為自定義類型導出顯示

[英]Haskell: Deriving Show for custom type

我有這種類型的定義:

data Operace = Op (Int->Int->Int) String (Int->Int->Int) deriving Show

我想將此類型打印到交互式shell(GHCi)中。 應打印的所有內容都是String字段。

我試過這個:

instance Show Operace where
    show (Op op str inv) = show str

但我仍然堅持下去

No instance for (Show (Int -> Int -> Int))
  arising from the 'deriving' clause of a data type declaration
Possible fix:
  add an instance declaration for (Show (Int -> Int -> Int))
  or use a standalone 'deriving instance' declaration,
       so you can specify the instance context yourself
When deriving the instance for (Show Operace)

我不想為Show (Int->Int->Int)添加Show ,我想要打印的只是字符串。

感謝幫助!

編輯:

為了將來參考,修復版本是:

data Operace = Op (Int->Int->Int) String (Int->Int->Int)

instance Show Operace where
    show (Op _ str _) = str

您所做的實例聲明是正確的方法。 您似乎忘記從原始data聲明中刪除該錯誤的deriving子句。

data Operace = Op (Int->Int->Int) String (Int->Int->Int)

instance Show Operace where
   show (Op op str inv) = show str

您可以派生Show ,首先導入Text.Show.Functions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM