Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Option Type

Option Type

Swift Option Type pitfalls

wagyu298

June 01, 2016
Tweet

More Decks by wagyu298

Other Decks in Programming

Transcript

  1. ࠓ೔ͷϙΠϯτ w ओʹ4XJGUͷ࿩͠·͢ w ࠷ۙͷ/VMM͕୅ೖͰ͖ͳ͍ϓϩάϥϛϯάݴޠ͕૿͑ͯΔ w )BTLFMM 4DBMB ' 3VTU

    4XJGU ,PUMJO FUD w ࣮ݱ͢Δ࢓૊ΈͷҰ͕ͭ0QUJPO5ZQF w ʢ4XJGUͰ͸0QUJPOBMɺ,PUMJOͰ͸/VMMBCMFͱݴ͍·͢ʣ w ࠷ۙͷྲྀߦΓͳͷͰ+BWB4DSJQUͱ͔ʹ΋͖͡ʹೖΔ͔΋ w ϥΠϒϥϦϕʔεͩͱOPEFPQUJPOTͱ͔͋ΔΜ͚ͩͲݴޠαϙʔτ͕ͳ͍ ͱ͠ΜͲ͍ w ࢖ͬͯΈΔͱͪΐ͍ͪΐ͍τϥοϓ͕͋ΔͷͰͦͷลΛڞ༗
  2. CJMMJPOEPMMBSNJTUBLF w *DBMMJUNZCJMMJPOEPMMBSNJTUBLF*UXBTUIFJOWFOUJPOPGUIFOVMMSFGFSFODFJO"U UIBUUJNF *XBTEFTJHOJOHUIFpSTUDPNQSFIFOTJWFUZQFTZTUFNGPSSFGFSFODFTJOBO PCKFDUPSJFOUFEMBOHVBHF "-(0-8 .ZHPBMXBTUPFOTVSFUIBUBMMVTFPGSFGFSFODFT TIPVMECFBCTPMVUFMZTBGF XJUIDIFDLJOHQFSGPSNFEBVUPNBUJDBMMZCZUIFDPNQJMFS#VU*

    DPVMEOUSFTJTUUIFUFNQUBUJPOUPQVUJOBOVMMSFGFSFODF TJNQMZCFDBVTFJUXBTTPFBTZ UPJNQMFNFOU5IJTIBTMFEUPJOOVNFSBCMFFSSPST WVMOFSBCJMJUJFT BOETZTUFNDSBTIFT  XIJDIIBWFQSPCBCMZDBVTFEBCJMMJPOEPMMBSTPGQBJOBOEEBNBHFJOUIFMBTUGPSUZZFBST w ͦΕ͸ԯυϧʹ΋૬౰͢ΔࢲͷޡΓͩɻOVMMࢀরΛൃ໌ͨ͠ͷ͸೥ͷ͜ͱͩͬͨɻ౰࣌ɺ ࢲ͸ΦϒδΣΫτࢦ޲ݴޠ "-(0-8 ʹ͓͚ΔࢀরͷͨΊͷแׅతܕγεςϜΛઃܭ͍ͯͨ͠ɻ ໨ඪ͸ɺίϯύΠϥͰͷࣗಈνΣοΫͰશͯͷࢀর͕׬શʹ҆શͰ͋Δ͜ͱΛอূ͢Δ͜ͱͩͬ ͨɻ͔͠͠ɺࢲ͸୯ʹͦΕ͕༰қͩͱ͍͏͚ͩͰɺແޮͳࢀরΛؚΊΔ༠࿭ʹ఍߅Ͱ͖ͳ͔ͬͨɻ ͜Ε͸ɺޙʹ਺͑੾Εͳ͍աͪɺ੬ऑੑɺγεςϜΫϥογϡΛҾ͖ى͜͠ɺաڈ೥ؒͰ ԯυϧ૬౰ͷۤ௧ͱଛ֐ΛҾ͖ىͨ͜͠ͱΈΒΕΔɻ w IUUQXXXJOGPRDPNQSFTFOUBUJPOT/VMM3FGFSFODFT5IF#JMMJPO%PMMBS.JTUBLF5POZ )PBSF
  3. /VMMFSSPS w 4XJGUͷී௨ OPOPQUJPOBM ͷม਺͸/VMMඇڐ༰Ͱ͢ w 4XJGUͰ͸OJM͕/VMMͰ͢ w OJMΛม਺ʹ୅ೖ͢ΔͱΤϥʔʹͳΓ·͢ w

    ద౰ʹ4XJGU৮Δͱଟ෼࠷ॳʹૺ۰͢ΔίϯύΠϥΤϥʔ w Α͘෼͔ͬͯͳ͍ͱ௒ΠϥΠϥ͢Δ w 4XJGUݴޠʹ૊Έࠐ·Ε͍ͯΔػೳͰ͢
  4. NPOBE WBST4USJOH 999 WBSUTNBQ\  """ ^ QSJOU U 

    UTqBU.BQ\  """ ^ QSJOU U  WBSVTNBQ\ 0QUJPOBM4USJOH  """  ^ QSJOU V  UTqBU.BQ\ 0QUJPOBM4USJOH  """  ^ QSJOU U
  5. 0QUJPO5ZQFͷఆٛ w 0QUJPO5ZQF͸/POF͔4PNF WBMVF Λฦ͢ 5BHHFE6OJPO λά෇͖ڞ༻ମ Ͱ͢ return: A

    ! A? = a 7! Just a bind: A? ! ( A ! B? ) ! B? = a 7! f 7! ( Nothing if a = Nothing f a0 if a = Just a0