J'ai un tas de fonctions comme:Haskell - emballages de type unification
f1 :: String -> String -> ... -> String ->()
f1 a b ... z = g [("a", a), ("b", b), ... ("z", z)]
...
fn :: String -> Int -> String -> ... -> String ->()
fn a b ... z = g [("a", a), ("b", show b), ... ("z", z)]
Ainsi, l'utilisateur peut les appeler comme f1 "abc" "def"
. Je ne veux pas qu'il le fasse parce qu'il peut facilement échanger "abc" et "def" par erreur (et Dieu sait combien de temps serait gaspillé pendant le débogage). Je veux lui transmettre des arguments comme fk (A "abc") (B "def")
Pour autant que je peux voir, il y a 2 options:
Massive construction
data
et fonction massive Déballez:data Value = A String | B String | C Int | D String ... unpack :: Value -> String unpack (A a) = a unpack (B b) = b unpack (C c) = show c unpack (D c) = d
beaucoup de code.
communs et newtypes classe de types:
EDIT: D'accord, alors, nous pouvons utiliserGeneralizedNewtypeDeriving
dans ce cas simple.{-# LANGUAGE GeneralizedNewtypeDeriving #-} class Value a where unpack :: a -> String instance Value String where unpack = id instance Value Int where unpack = show newtype A = A String deriving Value newtype B = B String deriving Value newtype C = C Int deriving Value newtype D = D String deriving Value ...
est beaucoup mieux mais tous
fk
ressembleraitfk a b ... z = g [("a", unpack a), ("b", unpack b), ... ("z", unpack z)]
Beaucoup de code et la duplication.
Ce que je veux est un truc magique qui me permettrait:
fk a b ... z = g [("a", a), ("b", b), ... ("z", z)]
g = h . map (second unpack)