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. 0QUJPO5ZQF !XBHZV

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

    4XJGU ,PUMJO FUD w ࣮ݱ͢Δ࢓૊ΈͷҰ͕ͭ0QUJPO5ZQF w ʢ4XJGUͰ͸0QUJPOBMɺ,PUMJOͰ͸/VMMBCMFͱݴ͍·͢ʣ w ࠷ۙͷྲྀߦΓͳͷͰ+BWB4DSJQUͱ͔ʹ΋͖͡ʹೖΔ͔΋ w ϥΠϒϥϦϕʔεͩͱOPEFPQUJPOTͱ͔͋ΔΜ͚ͩͲݴޠαϙʔτ͕ͳ͍ ͱ͠ΜͲ͍ w ࢖ͬͯΈΔͱͪΐ͍ͪΐ͍τϥοϓ͕͋ΔͷͰͦͷลΛڞ༗
  3. 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
  4. 4XJGU w 4XJGUJTBHFOFSBMQVSQPTFQSPHSBNNJOH MBOHVBHFCVJMUVTJOHBNPEFSOBQQSPBDIUP TBGFUZ QFSGPSNBODF BOETPGUXBSFEFTJHO QBUUFSOT w IUUQTTXJGUPSHBCPVU

  5. ҎԼɺ4XJGUͷσϞΛ ަ͑ͳ͕Βߦ͖·͢

  6. /VMMFSSPS w 4XJGUͷී௨ OPOPQUJPOBM ͷม਺͸/VMMඇڐ༰Ͱ͢ w 4XJGUͰ͸OJM͕/VMMͰ͢ w OJMΛม਺ʹ୅ೖ͢ΔͱΤϥʔʹͳΓ·͢ w

    ద౰ʹ4XJGU৮Δͱଟ෼࠷ॳʹૺ۰͢ΔίϯύΠϥΤϥʔ w Α͘෼͔ͬͯͳ͍ͱ௒ΠϥΠϥ͢Δ w 4XJGUݴޠʹ૊Έࠐ·Ε͍ͯΔػೳͰ͢
  7. /VMMFSSPS WBSTUS4USJOHOJM

  8. ॳظԽ w ม਺͸ඇOJM஋ͰॳظԽ͠·͢ w ʢͪͳΈʹ4XJGUͰ͸WBS͕WBSJBCMFɺMFU͕ DPOTUBOUͳͷͰ͕͢ɺҰൠతͳม਺ʗఆ਺ͱ؆୯ ʹଊ͑Δͱ৭ʑ΍΍͍͜͠࿩͕͋Γ·͢ͷͰɺWBS MFUΛ۠ผͤͣม਺ͱදه͍ͯ͠·͢ʣ

  9. ॳظԽ WBSTUS4USJOHl999z

  10. 0QUJPOBM w OJMͰॳظԽ͍ͨ͠৔߹͸0QUJPOBMܕม਺Λ࢖༻ ͠·͢ w ܕͷޙΖʹ Λ෇͚Δͱ0QUJPOBMܕม਺ʹͳΓ· ͢

  11. 0QUJPOBM WBSTUS4USJOH OJM QSJOU TUS

  12. 0QUJPOBM w ඇ0QUJPOBMܕม਺ʹ0QUJPOBMܕม਺Λ୅ೖ͢Δ ͜ͱ͸Ͱ͖·ͤΜ w 4XJGUΛ࢖ͬͯద౰ʹίʔσΟϯά͢Δͱଟ෼࠷ॳ ʹૺ۰͢Δҙຯෆ໌ͳίϯύΠϥΤϥʔ

  13. 0QUJPOBM GVODG TUS4USJOH \ QSJOU TUS  ^ WBSTUS4USJOH OJM

    G TUS
  14. 6OXSBQ w 0QUJPOBMܕม਺͔Βඇ0QUJPOBMܕͷ஋ΛऔΓग़ ͢͜ͱΛ4XJGUͰ͸6OXSBQͱݺͼ·͢ɻ w 6OXSBQ͢Δʹ͸0QUJPOBMܕม਺ͷޙΖʹΛ෇ ͚·͢ɻ

  15. 6OXSBQ GVODG TUS4USJOH \ QSJOU TUS  ^ WBSTUS4USJOH """

    G TUS
  16. 0QUJPOBM$IBJOJOH w 0QUJPOBMܕม਺ͷϝϯό 1SPQFSUZNFUIPE ʹ ҆શʹΞΫηε͢Δํ๏Ͱ͢ɻ w 0QUJPOBMม਺ͷޙΖʹ Λ෇͚ͯϝϯόʹΞΫη ε͢ΔͱɺOJMඇOJM໰ΘͣϝϯόʹΞΫηεͰ͖

    ·͢ɻ
  17. 0QUJPOBM$IBJOJOH DMBTT5\ WBSTUS4USJOH999 ^ WBSU5 5  WBSTUSU TUS QSJOU

    TUS  UOJM TUSU TUS QSJOU TUS
  18. 0QUJPOBM$IBJOJOH w 0QUJPOBM$IBJOJOH͕ฦ͢஋͸0QUJPOBMܕʹͳΓ ·͢ w 0QUJPOBM$IBJOJOHͰऔಘͨ͠஋Λඇ0QUJPOBMܕ ม਺΍ඇ0QUJPOBMܕҾ਺Λड͚औΔؔ਺ʹ౉ͦ͏ ͱ͢ΔͱίϯύΠϧΤϥʔ͕ൃੜ͠·͢ w 4XJGU͕෼͔͖ͬͯͨͱࢥ͏͜Ζʹૺ۰͢Δͱࢥ

    ͍·͢
  19. 0QUJPOBM$IBJOJOH DMBTT5\ WBSTUS4USJOH999 ^ GVODG T4USJOH \ QSJOU T 

    ^ WBSU5 5  G U TUS
  20. 0QUJPOBM$IBJOJOH w ༨ஊͰ͕͢ w ͸͋͘·Ͱ΋0QUJPOBM$IBJOJOHͳͷͰ຤ඌ ʹ෇͚Ε·ͤΜ w $P⒎FF4DSJQUॻ͍ͯΔͱΑ͘෼͔Βͳ͘ͳΔ w ͸6OXSBQͳͷ෇͚Ε·͢

  21. 0QUJPOBM$IBJOJOH DMBTT5\ WBST4USJOH 99 ^ WBSD5  WBSTDT  WBSUDT

  22. JGMFUHVBSEFMTF w 0QUJPOBMܕͷม਺΍0QUJPOBM$IBJOJOHͨ͠஋ʹ ҆શʹΞΫηε͍ͨ͠৔߹͸JGMFU͔HVBSEFMTF ߏจ͕ศརͰ͢

  23. JGMFU DMBTT5\ WBSTUS4USJOH999 ^ GVODG T4USJOH \ QSJOU T 

    ^ WBSU5 5  JGMFUTU TUS\ G T  ^
  24. HVBSEFMTF GVODG T4USJOH \ HVBSEMFUUTFMTF\ SFUVSO ^ QSJOU U 

    ^ G OJM 
  25. *NQMJDJUMZ6OXSBQQFE 0QUJPOBM w *NQMJDJUMZ҉໧త w ͱ͔ Λॻ͔ͳͯ͘΋ɺ҉໧తʹ6OXSBQ͞Ε Δ0QUJPOBMܕͷ͜ͱͰ͢

  26. *NQMJDJUMZ6OXSBQQFE 0QUJPOBM WBST4USJOHOJM JGTOJM\ QSJOU T  ^

  27. NPOBE w 0QUJPOBMܕʹ͸NBQͱqBU.BQͱ͍͏ϝιου ͕ఆٛ͞Ε͍ͯ·͢ɻ w NBQ͸0QUJPOBMܕม਺͕ඇOJMΛ࣋ͭ৔߹ʹؔ਺ Λద༻͠0QUJPOBMܕͰฦ͠·͢ɻ w qBU.BQ͸0QUJPOBMܕม਺͕ඇOJMΛ࣋ͭ৔߹ʹ ؔ਺Λద༻͠ඇ0QUJPOBMܕͰฦ͠·͢ɻ

  28. NPOBE WBST4USJOH 999 WBSUTNBQ\  """ ^ QSJOU U 

    UTqBU.BQ\  """ ^ QSJOU U  WBSVTNBQ\ 0QUJPOBM4USJOH  """  ^ QSJOU V  UTqBU.BQ\ 0QUJPOBM4USJOH  """  ^ QSJOU U
  29. ࣮૷໘ͷ࿩ w 0QUJPOBM*NQMJDJUMZ6OXSBQQFE0QUJPOBMܕม ਺͸(FOFSJDͰ࣮૷͞Ε͍ͯ·͢ w ΍Λ࢖Θͣ(FOFSJDߏจͰม਺ఆٛ͢Δ͜ ͱ΋ՄೳͰ͢ w 4XJGUʹݶΒͣଞͷݴޠ΋ࣅ༷ͨͳײ͡Ͱ͢ 

  30. ࣮૷໘ͷ࿩ WBST0QUJPOBM4USJOH""" WBSU*NQMJDJUMZ6OXSBQQFE0QUJPOBM4USJOH"""

  31. ϓϩάϥϛϯάαϙʔτ w ͜ͷลͰOJMॳظԽΤϥʔͱ͔ग़ͯ͠·͢ w IUUQTHJUIVCDPNBQQMFTXJGUCMPCNBTUFS JODMVEFTXJGU"45 %JBHOPTUJDT4FNBEFG-- w IUUQTHJUIVCDPNBQQMFTXJGUCMPCNBTUFS MJC4FNB$4%JBHDQQ-

  32. 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
  33. ͓ΘΓ