Comment puis-je définir une fonction qui acceptera mon type et retournera son "synonyme" primitif? Par exemple:Constructeur de données inversées
newtype MyInt = MakeInt Int
Et je veux une fonction:
unMyInt :: MakeInt -> Int
Comment puis-je définir une fonction qui acceptera mon type et retournera son "synonyme" primitif? Par exemple:Constructeur de données inversées
newtype MyInt = MakeInt Int
Et je veux une fonction:
unMyInt :: MakeInt -> Int
Par modèle correspondant au constructeur:
unMyInt (MakeInt i) = i
L'autre (et plus de façon confortable parfois) est enregistrement syntaxe:
newtype myInt a = MyInt { unMyInt :: Int }
Cela définit automatiquement une fonction
unMyInt :: MyInt -> Int
Qui m'a rabaissé? Peut-il s'il vous plaît expliquer? – fuz
Merci, où puis-je lire ce que cette construction signifie? – Ramesh
@Ramesh: Dans n'importe quel didacticiel ou livre de haskell (recherchez le chapitre sur la correspondance de modèles). Cela signifie "pour tout i, le résultat de l'application de' unMyInt' à la valeur 'MakeInt i' est' i' ". – sepp2k
@Ramesh: Par exemple, jetez un oeil à la [section sur la correspondance de modèles] (http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html#deftypes.pattern) de Real World Haskell. – sepp2k