The problem is that showContainer is of type:
showContainer :: (Show a, Show b) => MyContainer ab -> String
But when you go through SecondVersion 1 , he does not know what b , because SecondVersion 1 works for any type of b ! When you pass in FirstVersion , it works fine, because since FirstVersion contains both a and b , there is never any ambiguity about what they should be.
So, since the compiler does not know what you need b , and does not know that choosing b does not affect showContainer (in the end, it affects the behavior when you pass FirstVersion , since it uses show for a value of type b ), it refuses.
What the error message says: a variable of type a0 1 is ambiguous, so add a type signature to tell me what it is. In this case, it doesn't matter what it is, so you can just set it to () :
startF = showContainer (SecondVersion 1 :: MyContainer Integer ())
You probably don't often encounter such errors, since the context in which you use the values usually results in the use of a specific b .
1 GHC, unfortunately, is not the best when choosing types of variables; if you gave showContainer explicit type signature, as I showed, then it would also use b in the error message.
ehird
source share