Note: there may be some typos (capitals etc) as my wireless card has broken on my computer and I cant install haskell on this one, so I am re-typing the code (and I dont have a memory stick xD)
I have been using
Config line values 2 2
to define a Config but I just doesn’t like this one function ‘start’. Anything that takes an f -> f works fine though…
data Config = Config {
line :: Line,
nums :: [Nums],
indent :: Indent,
run :: Run
} deriving (Eq, Show)
class (Result f) => Test f where
start :: Line -> [Nums] -> f
instance Test Config where
start line nums = Config line nums 0 0
If I run
> start 2 [0,0,0]
which should return
> Config 2 [0,0,0] 0 0
I get the error:
Ambiguous type variable `f0' in the constraint:
(Test f0) arising from a use of `start'
Probable fix: add a type signature that fixes these type variable(s)
running
> :t Config 2 [0,0,0] 0 0
gives
> Config 2 [0,0,0] 0 0 :: Config
which is correct
Well, just what the message says,
can have any type that is an instance of
Test. The compiler has no way of finding out without you telling it, either directly with a type signature,should work without problem, or by supplying context from which the type can be inferred,
should also work because the type can now be inferred by the type of
indentthat uses the result ofstart.You probably expected the compiler to choose the type
Configbecause at the moment, that’s the only instance ofTest. But the compiler never selects an instance because there is no other instance it knows of, since that could break code when other instances are added in different modules.