import Data.Maybe
f :: (Show t) => Maybe t -> IO()
f Nothing = putStrLn "Nothing!"
f (Just x) = putStrLn $ "The number is " ++ (show x)
main = do
f Nothing
Das gibt:Typ Fehler in Haskell, wenn Muster mit anzeigen Einschränkung passend gegen Vielleicht
foo.hs:7:3:
No instance for (Show a0) arising from a use of ‘f’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 22 others
In a stmt of a 'do' block: f Nothing
In the expression: do { f Nothing }
In an equation for ‘main’: main = do { f Nothing }
foo.hs:7:3:
No instance for (Show a0) arising from a use of ‘f’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 22 others
In a stmt of a 'do' block: f Nothing
In the expression: do { f Nothing }
In an equation for ‘main’: main = do { f Nothing }
Wie kann dieser Frieden von Code festgelegt werden? Der Punkt ist, ein showable sicher zu drucken. Ich dachte, dass (t) zeigen könnte => Vielleicht könnte t in sowohl Nothing und Just x destrukturiert werden.
Um Ihnen einen Hinweis zu geben, warum dieser Fehler existiert: Vergleichen Sie 'show ([] :: [Char])' und 'show ([] :: [Int])'. Dies macht es sehr deutlich, warum z.B. 'show []' ist mehrdeutig: Soll es den vorherigen oder den letzten Wert ergeben? Selbst für "Container", die keine Werte enthalten, muss man den Typ der enthaltenen Elemente festlegen, bevor das Verhalten von "show" bestimmt wird. –