ãã®èšäºã®åŸã«ã¯çç£çãªè°è«ãç¶ããŸããããHaskellã§ã®newtypeæ§é ã®æ£ãã䜿çšã«ã€ããŠã³ã³ã»ã³ãµã¹ãåŸãããšãã§ããŸããã§ãããèãæ¹ã¯éåžžã«åçŽã§ããnewtypeããŒã¯ãŒãã¯ãååã¯ç°ãªããŸããã代衚çã«ã¯ã©ããããã¿ã€ããšåçã®ã©ãããŒã¿ã€ãã宣èšããŸããäžèŠãããšãããã¯åå®å šæ§ãéæããããã®ç解ã§ããæ¹æ³ã§ããããšãã°ãnewtype宣èšã䜿çšããŠé»åã¡ãŒã«ã¢ãã¬ã¹ã®ã¿ã€ããå®çŸ©ããæ¹æ³ãæ€èšããŠãã ããã
newtype EmailAddress = EmailAddress Text
ãã®ããªãã¯ã¯ç§ãã¡ã«äœããã®æå³ãäžããã¹ããŒãã³ã³ã¹ãã©ã¯ã¿ãŒãšã«ãã»ã«åå¢çãšçµã¿åããããšãã»ãã¥ãªãã£ãæäŸããããšããã§ããŸããããããããã¯ãŸã£ããç°ãªãçš®é¡ã®å®å šæ§ã§ããããã¯ã¯ããã«åŒ±ãã1幎åã«ç§ãç¹å®ãããã®ãšã¯ç°ãªããŸããããèªäœãnewtypeã¯åãªããšã€ãªã¢ã¹ã§ãã
ååã¯åå®å šæ§ã§ã¯ãããŸãã©
å éšããã³å€éšã®ã»ãã¥ãªãã£
建èšçãªããŒã¿ã¢ããªã³ã°ïŒåã®èšäºã§è©³ãã説æããŸãïŒãšnewtypeã©ãããŒã®éãã瀺ã ããã«ãäŸãèŠãŠã¿ãŸããããã1ãã5ãŸã§ã®æŽæ°ããšããåãå¿ èŠã ãšããŸãã建èšçã¢ããªã³ã°ãžã®èªç¶ãªã¢ãããŒãã¯ã5ã€ã®ã±ãŒã¹ã§ã®åæã§ãã
data OneToFive
= One
| Two
| Three
| Four
| Five
次ã«ãIntãšOneToFiveã¿ã€ãã®éã§å€æããããã€ãã®é¢æ°ãèšè¿°ããŸãã
toOneToFive :: Int -> Maybe OneToFive
toOneToFive 1 = Just One
toOneToFive 2 = Just Two
toOneToFive 3 = Just Three
toOneToFive 4 = Just Four
toOneToFive 5 = Just Five
toOneToFive _ = Nothing
fromOneToFive :: OneToFive -> Int
fromOneToFive One = 1
fromOneToFive Two = 2
fromOneToFive Three = 3
fromOneToFive Four = 4
fromOneToFive Five = 5
ããã¯ãè¿°ã¹ãããç®æšãéæããã®ã«ååã§ãããå®éã«ã¯ããã®ãããªãã¯ãããžãŒã䜿çšããããšã¯äžäŸ¿ã§ãããŸã£ããæ°ããã¿ã€ããçºæããã®ã§ãHaskellãæäŸããéåžžã®æ°å€é¢æ°ãåå©çšããããšã¯ã§ããŸããããããã£ãŠãå€ãã®éçºè ã¯ä»£ããã«newtypeã©ãããŒã䜿çšããããšã奜ã¿ãŸãã
newtype OneToFive = OneToFive Int
æåã®ã±ãŒã¹ãšåæ§ã«ãé¢æ°toOneToFiveãšfromOneToFiveãåãã¿ã€ãã§å®£èšã§ããŸãã
toOneToFive :: Int -> Maybe OneToFive
toOneToFive n
| n >= 1 && n <= 5 = Just $ OneToFive n
| otherwise = Nothing
fromOneToFive :: OneToFive -> Int
fromOneToFive (OneToFive n) = n
ãããã®å®£èšãå¥ã®ã¢ãžã¥ãŒã«ã«é 眮ããOneToFiveã³ã³ã¹ãã©ã¯ã¿ãŒããšã¯ã¹ããŒãããªãããšãéžæããå ŽåãAPIã¯å®å šã«äº€æå¯èœã§ããnewtypeãªãã·ã§ã³ã¯ããåçŽã§ãããã¿ã€ãã»ãŒããªããã§ãããã ããããã¯å®å šã«çå®ã§ã¯ãããŸããã
OneToFiveå€ãåŒæ°ãšããŠåãé¢æ°ãäœæããŠãããšæ³åããŠã¿ãŸãããã建èšçã¢ããªã³ã°ã§ã¯ããã®ãããªé¢æ°ã«ã¯5ã€ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã®ãããããšã®ãã¿ãŒã³ãããã³ã°ãå¿ èŠã§ããGHCã¯ã次ã®å®çŸ©ãååã«åãå ¥ããŸãã
ordinal :: OneToFive -> Text
ordinal One = "first"
ordinal Two = "second"
ordinal Three = "third"
ordinal Four = "fourth"
ordinal Five = "fifth"
ãã¥ãŒã¿ã€ãã®è¡šç€ºãç°ãªããŸããNewtypeã¯äžéæãªã®ã§ãããã芳å¯ããå¯äžã®æ¹æ³ã¯Intã«æ»ãããšã§ãããã¡ãããIntã«ã¯1ã5以å€ã®å€ãã®å€ãå«ããããšãã§ãããããæ®ãã®å¯èœãªå€ã®ãã¿ãŒã³ãè¿œå ããå¿ èŠããããŸãã
ordinal :: OneToFive -> Text
ordinal n = case fromOneToFive n of
1 -> "first"
2 -> "second"
3 -> "third"
4 -> "fourth"
5 -> "fifth"
_ -> error "impossible: bad OneToFive value"
ãã®æ¶ç©ºã®äŸã§ã¯ãåé¡ãçºçããªãå¯èœæ§ããããŸããããããããã«ããããããã説æãããŠãã2ã€ã®ã¢ãããŒãã«ãã£ãŠæäŸãããä¿èšŒã®éèŠãªéãã瀺ããŠããŸãã
- 建èšçãªããŒã¿åã¯ããããªãçžäºäœçšã«å©çšã§ããããã«äžå€æ¡ä»¶ãä¿®æ£ããŸããããã«ãããåºæ°é¢æ°ã¯è¡šçŸã§ããªããªã£ããããç¡å¹ãªå€ãåŠçã§ããªããªããŸãã
- newtypeã©ãããŒã¯ãå€ãæ€èšŒããã¹ããŒãã³ã³ã¹ãã©ã¯ã¿ãŒãæäŸããŸããããã®æ€èšŒã®ããŒã«çµæã¯ãå¶åŸ¡ãããŒã«ã®ã¿äœ¿çšãããŸããé¢æ°ã®çµæãšããŠä¿åãããŸããããããã£ãŠããã®ãã§ãã¯ã®çµæãšå°å ¥ãããå¶éããã以äžäœ¿çšããããšã¯ã§ããŸããããã®åŸã®å®è¡äžã«ãIntåãšå¯Ÿè©±ããŸãã
å®å šæ§ããã§ãã¯ããããšã¯äžå¿ èŠãªã¹ãããã®ããã«æãããããããŸããããããã§ã¯ãããŸããããã°ãæªçšããããšã§ãåã·ã¹ãã ã®è匱æ§ãææãããŠããŸãã OneToFiveããŒã¿åã«å¥ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãè¿œå ããå Žåã建èšçãªããŒã¿åã䜿çšããåºæ°ã®ããŒãžã§ã³ã¯ãã³ã³ãã€ã«æã«ããã«ç¶²çŸ çã§ã¯ãªããªããŸãããã®éãnewtypeã©ãããŒã䜿çšããå¥ã®ããŒãžã§ã³ã¯ã³ã³ãã€ã«ãç¶ããŸãããå®è¡æã«äžæããäžå¯èœãªã·ããªãªã«ãªããŸãã
ããã¯ãã¹ãŠã建èšçã¢ããªã³ã°ãæ¬è³ªçã«åå®å šã§ãããšããäºå®ã®çµæã§ããã€ãŸããã»ãã¥ãªãã£ããããã£ã¯å宣èšã«ãã£ãŠæäŸãããŸããç¡å¹ãªå€ãè¡šãããšã¯å®éã«ã¯äžå¯èœã§ãïŒ5ã€ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã®ããããã䜿çšããŠ6ã衚瀺ããããšã¯ã§ããŸããã
ããã¯ãintãšæ¬è³ªçãªæå³äžã®éãããªããããnewtype宣èšã«ã¯é©çšãããŸããããã®å€ã¯ãå·§åŠãªtoOneToFiveã³ã³ã¹ãã©ã¯ã¿ãŒãä»ããŠå€éšããæå®ãããŸããnewtypeã«ãã£ãŠæ瀺ãããã»ãã³ãã£ãã¯ã®éãã¯ãåã·ã¹ãã ããã¯èŠããŸãããéçºè ã¯ããã念é ã«çœ®ããŠããŸãã
空ã§ãªããªã¹ãã®åæ€èš
OneToFiveããŒã¿åãçºæãããŸããããä»ã®ããçŸå®çãªã·ããªãªã«ãåæ§ã®èæ ®äºé ãé©çšãããŸãã以åã«æžããNonEmptyã«ã€ããŠèããŠã¿ãŸãããã
data NonEmpty a = a :| [a]
æ確ã«ããããã«ãéåžžã®ãªã¹ããšæ¯èŒããŠãknewtypeãä»ããŠå®£èšãããNonEmptyã®ããŒãžã§ã³ãæ³åããŠã¿ãŸããããéåžžã®ã¹ããŒãã³ã³ã¹ãã©ã¯ã¿ãŒæŠç¥ã䜿çšããŠãç®çã®ç©ºã§ãªãããããã£ãæäŸã§ããŸãã
newtype NonEmpty a = NonEmpty [a]
nonEmpty :: [a] -> Maybe (NonEmpty a)
nonEmpty [] = Nothing
nonEmpty xs = Just $ NonEmpty xs
instance Foldable NonEmpty where
toList (NonEmpty xs) = xs
OneToFiveãšåæ§ã«ããã®æ å ±ãåã·ã¹ãã ã«æ ŒçŽã§ããªãå Žåã®çµæãããã«çºèŠã§ããŸããNonEmptyã䜿çšããŠå®å šãªããŒãžã§ã³ã®headãäœæãããã£ãã®ã§ãããnewtypeããŒãžã§ã³ã«ã¯å¥ã®ã¹ããŒãã¡ã³ããå¿ èŠã§ãã
head :: NonEmpty a -> a
head xs = case toList xs of
x:_ -> x
[] -> error "impossible: empty NonEmpty value"
ããã¯åé¡ã§ã¯ãªãããã§ãããã®ãããªç¶æ³ãçºçããå¯èœæ§ã¯éåžžã«äœãã§ãããããããã®ãããªè°è«ã¯ãNonEmptyãå®çŸ©ããã¢ãžã¥ãŒã«ã®æ£ãããä¿¡ããããšã«å®å šã«äŸåããŠããŸããã建èšçãªå®çŸ©ã§ã¯ãGHCåãã§ãã¯ãä¿¡é Œããã ãã§æžã¿ãŸããããã©ã«ãã§ã¯åãã§ãã¯ãæ£ããæ©èœãããšæ³å®ããŠãããããåŸè ã®æ¹ã説åŸåã®ãã蚌æ ã§ãã
ããŒã¯ã³ãšããŠã®ãã¥ãŒã¿ã€ã
ããªãããã¥ãŒã¿ã€ããæããŠãããªãããã®ãããã¯ã¯ã€ã©ã€ã©ãããããããŸããããã¥ãŒã¿ã€ããã³ã¡ã³ããããåªããŠãããšããæå³ã§ã¯ãããŸããããã³ã¡ã³ãã¯ã¿ã€ããã§ãã¯ã«å¹æçã§ãã幞ããªããšã«ãç¶æ³ã¯ããã»ã©æªãã¯ãããŸããããã¥ãŒã¿ã€ãã¯ã»ãã¥ãªãã£ã匱ããå¯èœæ§ããããŸãã
æœè±¡åã®å¢çã¯ããã¥ãŒã¿ã€ãã«å€§ããªã»ãã¥ãªãã£äžã®å©ç¹ããããããŸãã newtypeã³ã³ã¹ãã©ã¯ã¿ãŒããšã¯ã¹ããŒããããªãå Žåãä»ã®ã¢ãžã¥ãŒã«ã«å¯ŸããŠäžéæã«ãªããŸãããã¥ãŒã¿ã€ããå®çŸ©ããã¢ãžã¥ãŒã«ïŒã€ãŸãããããŒã ã¢ãžã¥ãŒã«ãïŒã¯ããããå©çšããŠãã¯ã©ã€ã¢ã³ããå®å šãªAPIã«å¶éããããšã«ãããå éšäžå€æ¡ä»¶ãé©çšãããä¿¡é Œå¢çãäœæã§ããŸãã
äžèšã®NonEmptyã®äŸã䜿çšããŠããã®ãã¯ãããžãŒã説æã§ããŸããä»ã®ãšãããNonEmptyã³ã³ã¹ãã©ã¯ã¿ãŒã®ãšã¯ã¹ããŒãã¯æ§ããŠããããæäœãšããŒã«æäœãæäŸããŸããããç§ãã¡ã¯åœŒããé©åã«åããŠãããšä¿¡ããŠããŸãïŒ
module Data.List.NonEmpty.Newtype
( NonEmpty
, cons
, nonEmpty
, head
, tail
) where
newtype NonEmpty a = NonEmpty [a]
cons :: a -> [a] -> NonEmpty a
cons x xs = NonEmpty (x:xs)
nonEmpty :: [a] -> Maybe (NonEmpty a)
nonEmpty [] = Nothing
nonEmpty xs = Just $ NonEmpty xs
head :: NonEmpty a -> a
head (NonEmpty (x:_)) = x
head (NonEmpty []) = error "impossible: empty NonEmpty value"
tail :: NonEmpty a -> [a]
tail (NonEmpty (_:xs)) = xs
tail (NonEmpty []) = error "impossible: empty NonEmpty value"
NonEmptyå€ãäœæãŸãã¯äœ¿çšããå¯äžã®æ¹æ³ã¯ããšã¯ã¹ããŒããããData.List.NonEmpty APIã®é¢æ°ã䜿çšããããšã§ãããããäžèšã®å®è£ ã¯ãã¯ã©ã€ã¢ã³ããé空ã®äžå€æ¡ä»¶ã«éåããã®ãé²ããŸããäžéæãªãã¥ãŒã¿ã€ãã®å€ã¯ããŒã¯ã³ã®ãããªãã®ã§ãïŒå®è£ ã¢ãžã¥ãŒã«ã¯ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãä»ããŠããŒã¯ã³ãçºè¡ãããããã®ããŒã¯ã³ã«ã¯å éšçãªæå³ããããŸããããããã䜿çšããŠæçšãªããšãè¡ãå¯äžã®æ¹æ³ã¯ããããã䜿çšããã¢ãžã¥ãŒã«å ã®é¢æ°ã§ãããã䜿çšã§ããããã«ãããããã«å«ãŸããå€ãååŸããããšã§ãããã®å Žåããããã®é¢æ°ã¯ããããšããŒã«ã§ãã
ãã®ã¢ãããŒãã¯ã建èšçãªããŒã¿åã䜿çšãããããå¹ççã§ã¯ãããŸãããããã¯ãééã£ãŠããå¯èœæ§ãããã誀ã£ãŠç¡å¹ãªNonEmpty []å€ãäœæããæ段ãæäŸããããã§ãããã®ãããåå®å šæ§ãžã®ãã¥ãŒã¿ã€ãã®ã¢ãããŒãèªäœã¯ãæãŸããäžå€æ¡ä»¶ãæãç«ã€ããšã蚌æãããã®ã§ã¯ãããŸããã
ãã ãããã®ã¢ãããŒãã§ã¯ãå®çŸ©ã¢ãžã¥ãŒã«ã®äžå€éåãçºçããå¯èœæ§ã®ããé åãå¶éãããŸããäžå€æ¡ä»¶ãå®éã«æç«ããããšã確èªããã«ã¯ããã¡ãžã³ã°ææ³ã䜿çšããŠã¢ãžã¥ãŒã«APIããã¹ãããããããããã£ã«åºã¥ããŠãã¹ãããå¿ èŠããããŸãã
ãã®åŠ¥åã¯éåžžã«åœ¹ç«ã¡ãŸãã建èšçãªããŒã¿ã¢ããªã³ã°ã䜿çšããŠäžå€æ¡ä»¶ãä¿èšŒããããšã¯é£ãããããåžžã«å®çšçã§ãããšã¯éããŸããããã ããäžå€æ¡ä»¶ãå£ãã¡ã«ããºã ã誀ã£ãŠæäŸããªãããã«æ³šæããå¿ èŠããããŸããããšãã°ãéçºè ã¯ãNonEmptyã®Genericåã¯ã©ã¹ãã掟çããGHCã³ã³ãããšã³ã¹åã¯ã©ã¹ãå©çšã§ããŸãã
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics (Generic)
newtype NonEmpty a = NonEmpty [a]
deriving (Generic)
ãã£ã1è¡ã§ãæœè±¡åã®å¢çãè¶ããããã®ç°¡åãªã¡ã«ããºã ãæäŸãããŸãã
ghci> GHC.Generics.to @(NonEmpty ()) (M1 $ M1 $ M1 $ K1 [])
NonEmpty []
掟çãžã§ããªãã¯ã€ã³ã¹ã¿ã³ã¹ã¯åºæ¬çã«æœè±¡åãç Žãããããã®äŸã¯å®éã«ã¯äžå¯èœã§ããããã«ããã®ãããªåé¡ã¯ãä»ã®ããŸãæçœã§ã¯ãªãç¶æ³ã§çºçããå¯èœæ§ããããŸããããšãã°ã掟çããReadã€ã³ã¹ã¿ã³ã¹ã®å ŽåïŒ
ghci> read @(NonEmpty ()) "NonEmpty []"
NonEmpty []
äžéšã®èªè ã«ã¯ããããã®ãã©ããã¯åœããåã®ããã«èŠãããããããŸãããããã®ãããªè匱æ§ã¯éåžžã«äžè¬çã§ããç¹ã«ãããè€éãªäžå€æ¡ä»¶ãæã€ããŒã¿åã®å Žåãã¢ãžã¥ãŒã«å®è£ ã§ãµããŒããããŠãããã©ãããå€æããã®ãé£ããå ŽåããããŸãããã®æ¹æ³ãé©åã«äœ¿çšããã«ã¯ã泚æãšæ³šæãå¿ èŠã§ãã
- ãã¹ãŠã®äžå€æ¡ä»¶ã¯ããã©ã¹ãããã¢ãžã¥ãŒã«ã®ã¡ã³ããã«å¯ŸããŠæ確ã§ããå¿ èŠããããŸããNonEmptyãªã©ã®åçŽãªåã®å Žåãäžå€æ¡ä»¶ã¯æããã§ãããããè€éãªåã®å Žåãã³ã¡ã³ããå¿ èŠã§ãã
- ãã©ã¹ãããã¢ãžã¥ãŒã«ãžã®ãã¹ãŠã®å€æŽã¯ãç®çã®äžå€æ¡ä»¶ã匱ããå¯èœæ§ãããããããã§ãã¯ããå¿ èŠããããŸãã
- 誀çšããå Žåã«äžå€æ¡ä»¶ãå±éºã«ãããå¯èœæ§ã®ããå®å šã§ãªãæãç©Žãè¿œå ããããšã¯æ§ããŠãã ããã
- ä¿¡é Œã§ããé åãå°ããä¿ã€ããã«ãå®æçãªãªãã¡ã¯ã¿ãªã³ã°ãå¿ èŠã«ãªãå ŽåããããŸããããããªããšãæéã®çµéãšãšãã«çžäºäœçšã®ç¢ºçãæ¥æ¿ã«å¢å ããäžå€æ¡ä»¶ã®éåãçºçããŸãã
åæã«ããã®æ§é ã«ãã£ãŠæ£ããããŒã¿åã«ã¯ãäžèšã®åé¡ã¯ãããŸãããããŒã¿åã®å®çŸ©ãå€æŽããã«äžå€æ¡ä»¶ã«éåããããšã¯ã§ããŸãããããã¯ããã°ã©ã ã®æ®ãã®éšåã«åœ±é¿ããŸããåãã§ãã¯ã¯èªåçã«äžå€æ¡ä»¶ãé©çšãããããéçºè ã®åªåã¯å¿ èŠãããŸãããããã°ã©ã ã®ãã¹ãŠã®éšåãããŒã¿åã«ãã£ãŠèª²ããããå¶éã®å¯Ÿè±¡ãšãªãããããããã®ããŒã¿åã«ã¯ãä¿¡é Œã§ããã³ãŒããã¯ãããŸããã
ã©ã€ãã©ãªã§ã¯ãããè€éãªããŒã¿æ§é ãäœæããããã«äœ¿çšããããã«ãã£ã³ã°ãããã¯ãæäŸããããšãå€ããããã«ãã»ã«åãéããŠã»ãã¥ãªãã£ã®æ°ããæŠå¿µïŒnewtypeã®ãããã§ïŒã䜿çšããããšã¯çã«ããªã£ãŠããŸãããã®ãããªã©ã€ãã©ãªã¯éåžžãã¢ããªã±ãŒã·ã§ã³ã³ãŒããããå€ãã®èª¿æ»ãšç²Ÿæ»ãåããŸããç¹ã«ãå€æŽã®é »åºŠãã¯ããã«å°ãªãããã§ãã
ã¢ããªã±ãŒã·ã§ã³ã³ãŒãã§ã¯ããããã®ææ³ã¯äŸç¶ãšããŠæçšã§ãããæéã®çµéã«äŒŽãæ¬çªã³ãŒãããŒã¹ã®å€æŽã¯ã«ãã»ã«åã®å¢çã匱ãããããå¯èœãªå Žåã¯èšèšãåªå ããå¿ èŠããããŸãã
ãã¥ãŒã¿ã€ããä¹±çšããã³èª€çšã®ä»ã®äœ¿çš
åã®ã»ã¯ã·ã§ã³ã§ã¯ãnewtypeã®äž»ãªçšéã«ã€ããŠèª¬æããŸããããã ããå®éã«ã¯ããã¥ãŒã¿ã€ãã¯éåžžãäžèšãšã¯ç°ãªãæ¹æ³ã§äœ¿çšãããŸãããããã®ã¢ããªã±ãŒã·ã§ã³ã®ããã€ãã¯æ£åœåãããŸããäŸãã°ïŒ
- Haskellã§ã¯ãåã¯ã©ã¹ã®äžè²«æ§ã®èãæ¹ã«ãããååã¯ä»»æã®ã¯ã©ã¹ã®1ã€ã®ã€ã³ã¹ã¿ã³ã¹ã«å¶éãããŸããè€æ°ã®æçšãªã€ã³ã¹ã¿ã³ã¹ãèš±å¯ããã¿ã€ãã®å Žåãnewtypesã¯åŸæ¥ã®ãœãªã¥ãŒã·ã§ã³ã§ãããæ£åžžã«äœ¿çšã§ããŸããããšãã°ãData.Monoidã®newtypes SumãšProductã¯ãæ°å€åã«åœ¹ç«ã€Monoidã€ã³ã¹ã¿ã³ã¹ãæäŸããŸãã
- åæ§ã«ãnewtypesã䜿çšããŠãåãã©ã¡ãŒã¿ãŒãæ¿å ¥ãŸãã¯å€æŽã§ããŸããData.Bifunctor.Flipã®NewtypeFlipã¯ãBifunctoråŒæ°ã亀æããŠãFunctorã€ã³ã¹ã¿ã³ã¹ãåŒæ°ã®éã®é åºã§æ©èœã§ããããã«ããç°¡åãªäŸã§ãã
newtype Flip p a b = Flip { runFlip :: p b a }
Haskellã¯ãŸã ã¿ã€ãã¬ãã«ã®ã©ã ãåŒããµããŒãããŠããªãããããã®çš®ã®æäœã«ã¯ãã¥ãŒã¿ã€ããå¿ èŠã§ãã
- ééçãªãã¥ãŒã¿ã€ãã¯ãããã°ã©ã ã®ãªã¢ãŒãéšåéã§å€ãæž¡ãå¿ èŠããããäžéã³ãŒããå€ãæ€èšŒããçç±ããªãå Žåã«ãæªçšãé²ãããã«äœ¿çšã§ããŸããããšãã°ãç§å¯éµãå«ãByteStringãnewtypeïŒShowã€ã³ã¹ã¿ã³ã¹ãé€å€ïŒã§ã©ããããŠãã³ãŒãã誀ã£ãŠãã°ã«èšé²ãããããå ¬éããããããã®ãé²ãããšãã§ããŸãã
ãããã®æ £è¡ã¯ãã¹ãŠè¯ãã§ãããåå®å šæ§ãšã¯äœã®é¢ä¿ããããŸãããæåŸã®ãã€ã³ãã¯ã»ãã¥ãªãã£ãšééãããããšãå€ããè«çãšã©ãŒãåé¿ããããã«åã·ã¹ãã ã䜿çšããŸãããããããã®ãããªäœ¿çšãèåŸ ãé²ããšäž»åŒµããã®ã¯ééã£ãŠããã§ããããããã°ã©ã ã®ã©ã®éšåã§ãããã€ã§ãå€ã確èªã§ããŸãã
ããŸãã«ãé »ç¹ã«ããã®ã»ãã¥ãªãã£ã®å¹»æ³ã¯ããã¥ãŒã¿ã€ãã®é²éªšãªä¹±çšã«ã€ãªãããŸããããšãã°ãç§ãå人çã«äœ¿çšããŠããã³ãŒãããŒã¹ã®å®çŸ©ã¯æ¬¡ã®ãšããã§ãã
newtype ArgumentName = ArgumentName { unArgumentName :: GraphQL.Name }
deriving ( Show, Eq, FromJSON, ToJSON, FromJSONKey, ToJSONKey
, Hashable, ToTxt, Lift, Generic, NFData, Cacheable )
ãã®å Žåãnewtypeã¯ç¡æå³ãªã¹ãããã§ããæ©èœçã«ã¯ãNameåãšå®å šã«äºææ§ãããããã12åã®åã¯ã©ã¹ãçæãããŸãã newtypeã䜿çšãããŠããå Žåã¯åžžã«ãçµäºã¬ã³ãŒãããååŸããããšããã«æ¡åŒµãããŸãããããã£ãŠããã®å Žåãåå®å šæ§ã«ã¡ãªããã¯ãããŸãããããã«ããã£ãŒã«ãåããã§ã«ãã®åœ¹å²ãæ確ã«ããŠããå ŽåããªãnewtypeãArgumentNameãšããŠæå®ããã®ãã¯æ確ã§ã¯ãããŸããã
ãã®ãã¥ãŒã¿ã€ãã®äœ¿çšã¯ãäžçã®åé¡ïŒåé¡ïŒã®æ¹æ³ãšããŠåã·ã¹ãã ã䜿çšããããšããé¡æããçããŠããããã«æãããŸããåŒæ°åã¯äžè¬åãããå ·äœçã§ããããããã¡ããç¬èªã®åãå¿ èŠã§ãããã®ã¹ããŒãã¡ã³ãã¯çã«ããªã£ãŠããŸããããããééã£ãŠããŸãïŒåé¡æ³ã¯é¢å¿ã®ããé åãææžåããã®ã«åœ¹ç«ã¡ãŸããããããã¢ãã«åããã®ã«å¿ ããã圹ç«ã€ãšã¯éããŸãããããã°ã©ãã³ã°æã«ã¯ãããŸããŸãªç®çã§åã䜿çšããŸãã
- äž»ã«ãã¿ã€ãã¯å€éã®æ©èœã®éãã匷調ããŸããNonEmpty aåã®å€ã¯ãæ§é ãæ ¹æ¬çã«ç°ãªããè¿œå ã®æäœãå¯èœã§ããããã[a]åã®å€ãšã¯æ©èœçã«ç°ãªããŸãããã®æå³ã§ãåã¯æ§é çã§ãããããã¯ãããã°ã©ãã³ã°èšèªå ã®å€ãèšè¿°ããŸãã
- -, , . Distance Duration, - , , .
ãããã®ç®æšã¯äž¡æ¹ãšãå®çšçã§ããããšã«æ³šæããŠãã ããã圌ãã¯åã·ã¹ãã ãããŒã«ãšããŠç解ããŠããŸããéçåã·ã¹ãã ã¯æåéãããŒã«ã§ãããããããã¯ããªãèªç¶ãªæ 床ã§ããããã«ãããããããäžçãåé¡ããããã«åã䜿çšãããšãéåžžãArgumentNameã®ãããªåœ¹ã«ç«ããªããã€ãºãçºçããã«ããããããããã®èŠ³ç¹ã¯ç§ãã¡ã«ã¯çããããã«æãããŸãã
newtypeãå®å šã«ééçã§ãããå¿ èŠã«å¿ããŠã©ãããããŠå±éãããå Žåã¯ãããããããŸãå®çšçã§ã¯ãããŸããããã®ç¹å®ã®ã±ãŒã¹ã§ã¯ãåºå¥ãå®å šã«é€å€ããŠNameã䜿çšããŸãããç°ãªãã©ãã«ãæ確ãªç¶æ³ã§ã¯ããã€ã§ããšã€ãªã¢ã¹ã¿ã€ãã䜿çšã§ããŸãã
type ArgumentName = GraphQL.Name
ãããã®ãã¥ãŒã¿ã€ãã¯å®éã®ã·ã§ã«ã§ããè€æ°ã®ã¹ããããã¹ãããããããšã¯ã¿ã€ãã»ãŒãã§ã¯ãããŸãããç§ãä¿¡ããŠãã ãããéçºè ã¯äœãèããã«åãã§é£ã³è¶ããŸãã
çµè«ãšæšå¥šèªæž
ç§ã¯é·ãéããã®ãããã¯ã«é¢ããèšäºãæžããããšæã£ãŠããŸãããããã¯ããããHaskellã®ãã¥ãŒã¿ã€ãã«ã€ããŠã®éåžžã«çãããã³ãã§ããç§èªèº«ãHaskellã§çèšãç«ãŠãŠãããå®éã«ã¯åžžã«åæ§ã®åé¡ã«çŽé¢ããŠããããããã®ããã«äŒããããšã«ããŸãããå®éãäž»ãªã¢ã€ãã¢ã¯ã¯ããã«æ·±ããã®ã§ãã
Newtypesã¯ãã©ãããŒã¿ã€ããå®çŸ©ããããã®ã¡ã«ããºã ã®1ã€ã§ãããã®æŠå¿µã¯ãåçåä»ãã䜿çšããèšèªãå«ããã»ãŒãã¹ãŠã®èšèªã«ååšããŸãã Haskellãæžããªãå Žåããã®èšäºã®å€ãã¯éžæããèšèªã«åœãŠã¯ãŸãå¯èœæ§ããããŸããããã¯ãç§ãéå»1幎éã«ããŸããŸãªæ¹æ³ã§äŒããããšãã1ã€ã®ã¢ã€ãã¢ã®ç¶ãã§ãããšèšããŸããåã·ã¹ãã ã¯ããŒã«ã§ããã©ã®ã¿ã€ããå®éã«æäŸããã®ãããããŠããããå¹æçã«äœ¿çšããæ¹æ³ã«ã€ããŠãããæèããéäžããå¿ èŠããããŸãã
ãã®èšäºãæžããçç±ã¯ãæè¿å ¬éãããèšäºã ã¿ã°ä»ãã¯ãã¥ãŒã¿ã€ãã§ã¯ãªããã§ããã..ãããã¯çŽ æŽãããæçš¿ã§ãããç§ã¯å®å šã«äž»èŠãªã¢ã€ãã¢ãå ±æããŠããŸããããããèè ã¯ãã£ãšçå£ãªèããè¡šæããæ©äŒãéãããšæããŸãããå®éãTaggedã¯å®çŸ©äžæ°ããã¿ã€ãã§ãããããèšäºã®ã¿ã€ãã«ãééã£ãæ¹åã«é²ãã§ããŸããæ¬åœã®åé¡ã¯ããå°ãæ·±ããªããŸãã
ãã¥ãŒã¿ã€ãã¯æ³šææ·±ãé©çšãããšäŸ¿å©ã§ãããã»ãã¥ãªãã£ã¯ããã©ã«ãã®ããããã£ã§ã¯ãããŸãããç§ãã¡ã¯ããã©ãã£ãã¯ã³ãŒã³ãæ§æãããã©ã¹ããã¯ãããèªäœã§äº€éå®å šãæäŸãããšã¯èããŠããŸãããã³ãŒã³ãé©åãªã³ã³ããã¹ãã«é 眮ããããšãéèŠã§ããåãå¥ããªããã°ãnewtypesã¯åãªãã©ãã«ã§ãããååãä»ããæ¹æ³ã§ãã
ãããŠããã®ååã¯ã¿ã€ãã»ãŒãã§ã¯ãããŸããïŒ