J'ai une erreur de type type ambigu sur la définition de "procès" ci-dessous, je me demande s'il y a quelque chose qui peut être fait pour que cette situation fonctionne? Je veux vraiment traiter uniquement des instances et non des types de données explicites (tels que MO1, MO2 inclus ci-dessous).En utilisant haskell read et typeclasses - type ambigu variable variable
module Tc102 where
class (Show a, Read a) => MyObj a where
alpha :: a->String
beta :: a->Int
data MO1 = MO1 { a1 :: String, b1 :: Int } deriving (Show,Read)
data MO2 = MO2 { a2 :: String, b2 :: Int } deriving (Show,Read)
instance MyObj MO1 where
alpha = a1
beta = b1
instance MyObj MO2 where
alpha = a2
beta = b2
a = MO1 "a" 3
b = MO2 "b" 4
test :: MyObj a => a->String
test = alpha
showMe :: (MyObj a)=> a -> String
showMe = show
readMe :: (MyObj a) => String -> a
readMe = read
trial :: MyObj a => a -> String
trial = test . readMe . showMe
merci d'avance tous! Je crains cependant que je pourrais avoir besoin d'aller à une fonction d'assistance qui convertirait ancienne ADT aux « dernières versions ...
Simon
EDIT Pour clarifier, imaginez que je suis montrer à un fichier, puis plus tard recharger l'objet. Ensuite, la fonction que j'ai est plus comme
trial :: String -> Int
trial s = beta x
where x = readMe s
Avec un peu plus de type-foo, il y a quelque chose qui devrait fonctionner. Merci! –