ããã¯ãåçæ§é ãæã€ãªããžã§ã¯ããåŠçãããšãã®Optionalã¯ã©ã¹ã®äœ¿çšã«é¢ããã·ãªãŒãºã®5çªç®ãšæåŸã®èšäºã§ãã
æåã®èšäºã§ã¯ãOptionalã䜿çšã§ããªãããŸãã¯äœ¿çšããããªãç¶æ³ã§NullPointerExceptionãåé¿ããæ¹æ³ã«ã€ããŠèª¬æããŸããã
2çªç®ã®èšäºã§ã¯ãJava 8ã§ç»å ŽããOptionalã¯ã©ã¹ã®ã¡ãœããã«ã€ããŠèª¬æããŸã
ã3çªç®ã® èšäºã§ã¯ãJava 9ã§ã¯ã©ã¹ã«è¿œå ãããã¡ãœããã«ã€ããŠèª¬æããŸãã4
çªç®ã®èšäºã§ã¯ãããŒã¿ã®åŠçäžã«å€±æããå Žåã«Optionalã¯ã©ã¹ã®æ©èœãæ¡åŒµããã¯ã©ã¹ã玹ä»ããŸããã¡ãœããã§ã¯ããã®å€±æã«é¢ããæ
å ±ãåŠçããå¿
èŠããããŸãã
ãã®èšäºã§ã¯ããŒãå€ã衚瀺ãããå¯èœæ§ã®ãããã¹ãŠã®å Žæã§Optionalã䜿çšããå¿
èŠããããã©ããã®åé¡ã調ã¹ãŸãã ãŸãããã®ã¯ã©ã¹ã«é¢ããJava v Oracleèšèªã®ã¢ãŒããã¯ãã§ããBrian Goetzã®æèŠãè¿°ã¹ãŸãããã¡ãããååã®èšäºã§è¿°ã¹ãçŽæãæãããŸããã·ãªãŒãºã®ãã¹ãŠã®èšäºãèªããã¹ãŠã®èªè
ã«è²ŽéãªèŽãç©ããå§ãããŸãã
ãããããã¹ãŠãééã£ãŠããå¯èœæ§ããããŸã
ã¯ã©ã¹å
ã®ããŸããŸãªåœ¹å²ã§Optionalã䜿çšããããšã«é¢ããå®éçãªè³ªåãæ€èšããåã«ã1ã€ãå°ãããããã®ã§ãããç§ã®èŠ³ç¹ãããããšãå¿
èŠãªæŽå²çäœè«ã§ãã
FPïŒé¢æ°åããã°ã©ãã³ã°ïŒã®JavaèŠçŽ ãæ¡åŒµããåé¡ãé ã«æµ®ããã ãšããå¯èœãªè§£æ±ºçã®ç¯å²ã¯éåžžã«åºãããã«èŠããŸããã æ倧ããŒãžã§ã³ã§ã¯ãLISPãMLãHaskellãOCamlãFïŒãErlangãClojureãScalaãªã©ã®ããã°ã©ãã³ã°èšèªã§ãã¹ãããããæãéèŠãªFPãã©ãã€ã ã®å®è£
ã§Javaãæ¡åŒµããããšããããšãã§ããŸãã
ããã·ããªã¹ãã¢ãããŒãã®æ¹è©å®¶ã¯ãäžéšã®FPãã©ãã€ã ã®å®è£
ã¯ã以åã«èšèªã®å®è£
ã§è¡ãããã¢ãŒããã¯ãã£äžã®æ±ºå®ã«ãã£ãŠåŠšããããå¯èœæ§ããããšèŠåããŸããã ããã«ã圌ãã¯ãã¹ã¿ãŒããã«ã¯è€éããããã©ãã€ã ãå°å
¥ãããšããããã®äžé©åãªäœ¿çšã«ã€ãªããããšãæããŸããã ãããŠãããã¯ãã»ãšãã©ã®JavaãŠãŒã¶ãŒãæè¡é©æ°ã®å
šç¯å²ãç¡èŠããã ãããšè
ããŸããã
ãããã«ãããæçµçã«ããå®çšçããšå®£èšãããã¢ãããŒããæ®åããŸããã Java 8ã§ã¯ãFPã®äž»èŠãªèŠçŽ ã®ã¿ãã¹ã¬ãããé¢æ°ãã©ã ããããã³ãªãã·ã§ã³ã®åœ¢åŒã§è¡šç€ºãããŸããã
åè³ã¢ãããŒãã®äžéšãšããŠãå
¬åŒããã¥ã¡ã³ãã®Optionalã¯FPããã®ã¢ãããã©ãã€ã ã®å®è£
ã®1ã€ã§ãããšäž»åŒµããŠããŸããã§ããã ïŒåœ¹å²ã䞻匵ããªããšããããšã¯ã圹å²ãæãããªããšããããšã§ã¯ãããŸãã
ã詳现ã¯
ãã¡ããã芧ãã ããã
ãããŠããããé©çšããä»ã®å Žæã¯ïŒ
ãã®å Žåãäžéšã®ãŠãŒã¶ãŒã¯èªç¶ãªè³ªåãããŸãããã¯ã©ã¹ã®ã©ãã«nullã衚瀺ãããå Žåã§ãããªãã·ã§ã³ã䜿çšããå¿
èŠããããŸãã
èšåããããæ確åããªã©ã¯ã«ã®èšäºã«ã¯ãæ確ã«ããããïŒ
ããªãã¡ïŒç§ã®ç¿»èš³ã§ã¯ïŒïŒ
ãOptionalã¯ã©ã¹ã®ç®çã¯ãæœåšçã«nullãªããžã§ã¯ããžã®ãã¹ãŠã®åç
§ã眮ãæããããšã§ã¯ãããŸããã ç®æšã¯ã ããç解ããããAPIãéçºããŠãã¡ãœããã®çœ²åã«ãã£ãŠOptionalãè¿ãããããšãå€æã§ããããã«ããããšã§ãã ããã«ããããªãã·ã§ã³ãç©æ¥µçã«åæããŠãã®æå³ãææ¡ããå¿
èŠããããŸãã
ã ããããã ããããã©ãã§ãã¯ã©ã¹ã®ã©ã®ãå ŽæããŸãã¯ã圹å²ãã«Optionalã䜿çšã§ããŸããïŒ
ãã£ãŒã«ãå€ãšããŠãªãã·ã§ã³ïŒ
å
·äœçã«èŠãŠãã¯ã©ã¹å€æ°ïŒãã£ãŒã«ãïŒããå§ããŸãããã èšåãããèšäºãèŠãŠã¿ãŸãããã ãã®ãããªã³ãŒãã¯æ¬¡ã®ãšããã§ãã
public class Computer { private Optional<Soundcard> soundcard; public Optional<Soundcard> getSoundcard() { ... } ... }
ããïŒ ã¯ã©ã¹å€æ°ãšããŠOptionalã䜿çšã§ããŸããïŒ
å¯èœã§ãããããã§èŠããŠããå¿
èŠããããŸãã
- Optionalã¯ã©ã¹ã¯Serializebleã€ã³ã¿ãŒãã§ã€ã¹ãå®è£
ããŸããã ããã¯ããã®ããããã£ã§äœ¿çšããããã¬ãŒã ã¯ãŒã¯ã䜿çšããŠãå€æ°ãã¡ãã£ã¢ããæ£ããä¿åãŸãã¯èªã¿åãããªãããšãæå³ããŸãã
- ãªãã·ã§ã³ã¯ãããããå€ããŒã¹ã®ãªããžã§ã¯ãã§ãã ããã¯ãç¹ã«ããªããžã§ã¯ãã®ã€ã³ã¹ã¿ã³ã¹ãçžäºã«æ¯èŒããããã®ç¹å¥ãªã«ãŒã«ãæå³ããŸãã詳现ã«ã€ããŠã¯ã ãã¡ããã芧ãã ãã ã ããã¯ãå
éšã§Reflectionã¡ã«ããºã ã䜿çšããäžéšã®ãã¬ãŒã ã¯ãŒã¯ããã®ãããªãã£ãŒã«ãã誀ã£ãŠåŠçããå¯èœæ§ãããããšãæå³ããŸãã
ãããã£ãŠãã¯ã©ã¹ã®ãã£ãŒã«ãå€ã®ç®¡çè
ãšããŠOptionalã䜿çšããåã«ãã¯ã©ã¹ã®æœåšçãªãŠãŒã¶ãŒãããã®æºåãæŽã£ãŠããªããã¬ãŒã ã¯ãŒã¯ã䜿çšããŠã·ãªã¢ã«åãŸãã¯åŠçããå¿
èŠããªãããšã確èªããŠãã ããã
ãããã£ãŠãäžéšã®èè
ã¯ããããã®æœåšçãªé çã«å·»ã蟌ãŸããããªãã·ã§ã³ã§ã¯ãªããã£ãŒã«ãã«ãªããžã§ã¯ãèªäœãä¿åããã¯ã©ã¹ãããã°ã©ãã³ã°ãããšãã«ãŒãã«ãªãå¯èœæ§ãããããšãæãåºããŠãã ããã ãããŠããªããžã§ã¯ããæ»ãå€ãšããŠå€éšã«ã解æŸãããããšã«ãã£ãŠã®ã¿ããcaseããªãã·ã§ã³ã«å
¥ããŸãã
ã»ãã¿ãŒã§ãªãã·ã§ã³ã䜿çšãã
ããããæœåšçã«ãŒãã®ã¯ã©ã¹ãã£ãŒã«ããæå®ããæ¹æ³ã«ã€ããŠã¯ã©ãã§ããããã APIã§setSomeObjectïŒãªãã·ã§ã³ã®<SomeObject> optïŒã®ãããªã·ã°ããã£ãæã€ã¡ãœãããææ¡ãã䟡å€ãããã®ã¯ãã€ã§ããïŒ
æãããªã±ãŒã¹ã¯ãsetSomeObjectïŒSomeObject objïŒã¡ãœãããæã€æ¢åã®ã¯ã©ã¹ã®åšãã«ãã¡ãµãŒããæ§ç¯ããobjãnullã«ãªãå Žåã§ãã ãã®å Žåãå°ãªããšãã«ããŒããããã¡ãµãŒããã¡ãœããã§ã¯ãnullãªããžã§ã¯ãã䜿çšããå¯èœæ§ã«ã€ããŠãŠãŒã¶ãŒã«æ瀺çã«éç¥ãããŸãã
æ®ãã®ã±ãŒã¹ã§ã¯ãæž¡ããããã©ã¡ãŒã¿ãŒãnullã§ãªããšããå¶éïŒããã³å Žåã«ãã£ãŠã¯æ€èšŒïŒã䜿çšããŠãéåžžã®ã»ãã¿ãŒã¡ãœãããäœæããããšããå§ãããŸãã èšèšæ®µéã§ããã®å¶éã¯assertã§ç¢ºèªã§ããŸãã æ¬çªç°å¢ã§ç¢ºèªãããã©ããããããããªããè°è«ã®ç¯å²ãè¶
ããå¥ã®èå³æ·±ã質åã¯ã©ãã§ããããã
ãããã£ãŠãå€ãã®èè
ã«åŸã£ãŠãã»ãã¿ãŒã§ã®ãã«è»¢éãæ³å®ãããèš±å¯ããªãããšããå§ãããŸãã
ã©ã®ããã«ã泚ææ·±ãèªè
ã¯å«ã¶ã§ãããã ã·ãªãŒãºã®2çªç®ã®èšäºã§ã¯ãèè
èªèº«ããã®ã€ã³ã¿ãŒãã§ã€ã¹ã®äœ¿çšãææ¡ããŸããã
public interface IBoilerInput2 { void setAvailability(@Nullable CupOfWater water, boolean powerAvailable); }
nullã®å¯èœæ§ãããwaterãã©ã¡ãŒã¿ãŒã䜿çšããŠãä»ã§ã¯æªããšèšããŸããïŒ ã¯ã ããã§ãã äž»ãªåŽé¢ã«æ³šæãéäžããããã«ãã±ãŒã¹ã¹ã¿ãã£ã«åå ããŸããã ããããä»ã®éçºè
ã䜿çšã§ããå®éã®APIãéçºãããšãã¯ããããé¿ããå¿
èŠããããŸãã ããããã©ã®ããã«ïŒ
ç¶æ³ã詳ããèŠãŠã¿ãŸãããã ãã©ã¡ãŒã¿å€ãšããŠnullãæž¡ããšãéåžžããªããžã§ã¯ãã®æ§æãå€æŽããããæ©èœã®äžéšãç¡å¹ã«ããããåäœãæ°ããã¢ãŒãã«åãæ¿ãããããããšãæå³ããŸãã ãã®
ã¹ã€ããã説æããé©åãªçšèªãéžæããŸãã
äžèšã®ã€ã³ã¿ãŒãã§ã€ã¹ã¯ã次ã®ããã«å€æãããŸãã
public interface IBoilerInputX { void setWater(@Nonnull CupOfWater water); void setWaterDisabled(); void setPowerAvailable( boolean powerAvailable); }
æ°ããsetWaterDisabledã¡ãœããã¯ãããã€ã¹ã®åäœã®ä»åŸã®å€æŽãæ確ã«ç€ºããŸããããã¯ã以åã®ããŒãžã§ã³ã®IBoilerInput2ãšã¯ç°ãªããå€ããŒãã®ãã©ã¡ãŒã¿ãŒãåŒã³åºãããšã§éæãããŸããã
ãã©ã¡ãŒã¿ã°ã«ãŒãã§ãªãã·ã§ã³ã䜿çšãã
ãã ããå Žåã«ãã£ãŠã¯ãæŠå¿µçãŸãã¯æè¡çãªçç±ã«ãããç¹å®ã®ãã©ã¡ãŒã¿ãŒã®ã°ã«ãŒããã»ãã¿ãŒãšã¹ã€ããããåå¥ã®ãã¢ã«åå²ããããšã¯ã§ããããã©ã¡ãŒã¿ãŒã®ã°ã«ãŒãå
šäœã1åã®åŒã³åºãã§æž¡ãå¿
èŠããããŸãã ããã¯ã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒã§é »ç¹ã«çºçããŸãã ãã ããã°ã«ãŒãã®äžéšã®ãã©ã¡ãŒã¿ãŒã¯ååšããªããæºåãã§ããŠããªããªã©ã§ãã -ã€ãŸã nullã«ãªããŸãã ãã®å Žåããªãã·ã§ã³ã®ã±ãŒã¹ã䜿çšããããšã¯ãæœåšçã«ãã«ã®ãã©ã¡ãŒã¿ãŒãçŽæ¥æž¡ããããã¯ããã«åªããŠããŸãã 以åã®èšç®ã§èª€ã£ãŠéº»çºããŠããäžéšã®ãã©ã¡ãŒã¿ãŒãã¯ã©ã¹å
ã«åãŸããNPEãŸãã¯äžé©åãªåäœãåŒãèµ·ããå¯èœæ§ïŒãã®å Žåã¯å€§å¹
ã«æžå°ïŒã
æçµçãªæšå¥šäºé
äºåçãªçµæãå®çšçãªæšå¥šäºé
ã®åœ¢åŒã§èŠçŽããã«ã¯ïŒ
- nullã®å¯èœæ§ããããã©ã¡ãŒã¿ãŒãæ瀺çã«æž¡ãå¿
èŠãããã¡ãœããã®ãã¡ãµãŒããæ§ç¯ããå Žåã¯ãã»ãã¿ãŒãŸãã¯1ã€ã®ãã©ã¡ãŒã¿ãŒãæã€ã¡ãœããïŒã®ã¿ïŒã§Optionalã䜿çšããŸãã
- ã»ãšãã©ã®å Žåããã©ã¡ãŒã¿ãŒãnullå€ããšãããšãã§ããå Žåãnull以å€ã®å€ãšé¢é£ãããªããžã§ã¯ãã®åäœã¢ãŒãã®åãæ¿ãæ¹æ³ãå¶éããŠãã»ãã¿ãŒã«æž¡ããŸãã ã¹ã€ããå
ã§ã察å¿ãããã£ãŒã«ãã®å€ããªã»ããããŸãã
- ã¡ãœããã«å€ãã®ãã©ã¡ãŒã¿ãŒãå¿
èŠãªå Žåããã®äžéšã¯ãŒãå€ããšãããšãã§ããããããã®ãããªãã©ã¡ãŒã¿ãŒãOptionalã±ãŒã¹å
ã«æž¡ããŸãã ããã«ãããã¡ãœãããåŒã³åºãã¯ã©ã€ã¢ã³ãã¯ãã±ãŒã¹ã«ãªããžã§ã¯ãããåã蟌ããåã«ç¶æ³ãæ瀺çã«åŠçããŸãã
å£ã£ãæ»ãå€
ãã®ã·ãªãŒãºã®ç¬¬1åãšç¬¬2åã®èšäºã§ãæœåšçã«ãŒãã®æ»ãå€ãã±ãŒã¹ã«ããã±ãŒãžåããå¿
èŠãããããšãèªè
ã«é·ãé確信ãããŠããŸããã ãã€ãè¯ãã§ããïŒ æ®å¿µãªãããå
ããããšããã«ã¯åœ±ããããŸãã
ç¹ã«ãšã³ã¿ãŒãã©ã€ãºã·ã¹ãã ã§ã®è€éãªãªããžã§ã¯ãã¯ãå€ãã®å Žåãã²ãã¿ãŒã¡ãœãããåŒã³åºãããšã§å€éšããã¢ã¯ã»ã¹ã§ããåçŽãªãªããžã§ã¯ãã®éåäœãŸãã¯éå±€ã§ãã ãªããžã§ã¯ããã空ã®ããªããžã§ã¯ããè¿ããšããäºå®ïŒãããã±ãŒã¹ã«è©°ããããŠãããã©ããïŒã¯ãå€ãã®å Žåããã®ãããªãªããžã§ã¯ããéåäœãŸãã¯éå±€å
ã«ååšããªãããšãæå³ããŸããã å®éã®ã·ã¹ãã ã§ã¯ãããã¯å€ãã®å Žåããªããžã§ã¯ããäœããã®æå³ã§ãå£ã£ãŠãããããšããŸãã¯ãããå«ãéçŽãªããžã§ã¯ãã®æ§æãå€çã§ã®äœ¿çšãèš±å¯ããªãããšãæå³ããŸãã ãã®èŠ³ç¹ãããOptional.emptyïŒïŒãè¿ãããšã¯æ£åœåãããŸãã ãã ããå€éšãã¬ãŒã ã¯ãŒã¯ã䜿çšããŠïŒReflectionã䜿çšããããbycodeãæäœãããããã«ïŒãªããžã§ã¯ããå€éšã¡ãã£ã¢ã«ä¿åãããããããã¯ãŒã¯çµç±ã§éä¿¡ãããããå Žåããªããžã§ã¯ãã®ãå£çæ§ããŸãã¯ãããå«ããŠãããã®äžé©åãªæ§æãé害ã«ãªãããšã¯ãããŸããã Optionalã±ãŒã¹ãè¿ãã²ãã¿ãŒã¯ããã®å Žå䜿çšã§ããªãããšãããããŸãã ã©ãããŠïŒ äœã®ããã«æŠã£ãŠããŸãããïŒ
å®éããã¹ãŠãè¯ããªããŸããã ã¯ãã倧ããªéåäœã®äžéšãæ°žç¶åããããã®ç¹å¥ãªã¡ãœãããå®è£
ããå¿
èŠããããŸãã ãã ããåæã«1ã€ã®åå¥ã®ã€ã³ã¿ãŒãã§ã€ã¹ã§åéããå¥ã®ã€ã³ã¿ãŒãã§ã€ã¹ã§ããžãã¹ããžãã¯ãå®è£
ããæ¹æ³ãåéããããšããå§ãããŸãã ãããã£ãŠãåããªããžã§ã¯ãã䜿çšããããžãã¹é¢ãšæè¡é¢ãåé¢ã§ããŸãã
ããžãã¯ãã³ããŒã®ä»£ããã«ãªãã·ã§ã³
æ°åã®äžçã«ããããªããžã§ã¯ãã®äžçããã®ãã«ã¢ããã°ã¯ããéæ³ã®ãæ°åã§ãã ã€ãŸã ãéåžžã®ãæ°å€ãããæ ¹æ¬çã«ç°ãªãã»ãã³ãã£ã¯ã¹ãé¢é£ä»ãããããããªãã©ã¡ãŒã¿ãŒå€ã
æšæºã®Javaã©ã€ãã©ãªã§ãããããè¡ããŸãã Java.net.Socketã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒã®ããã¥ã¡ã³ãïŒ
public Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException
localPortãã©ã¡ãŒã¿ã«ã€ããŠããŒã以å€ã«èšå®ãããšã·ã¹ãã ã䜿çšããããšã瀺ããŠããŸãã ãã©ã¡ãŒã¿å€ã0ã®å Žåã空ããŠããããŒããæ¢ããŸãã
ãã©ã¡ãŒã¿å€ããŒãã§ãªãå ŽåãšãŒãã®å Žåã®ã¡ãœããã®åäœã¯æ ¹æ¬çã«ç°ãªããŸãã
å°æ¥ãã®ãããªç¶æ³ãé¿ããããã«ãJava 8ã§ã¯OptionalIntã¯ã©ã¹ãšOptionalDoubleã¯ã©ã¹ãå°å
¥ãããŸããã ã¡ãœãããæåããå Žåã«ç¹å®ã®æ°ãè¿ãå¿
èŠãããå Žåã«ãããã®ã¯ã©ã¹ã䜿çšããŸããã倱æããããšãå¯èœã§ãã
çµè«ã®ä»£ããã«ãèªåã§èããŠãã ããïŒ
Optionalã¯ã©ã¹ã®ã¡ãœããã䜿çšããããã®ãªãã·ã§ã³ã説æããããã«5ã€ã®èšäºãæ§ããŸããã Javaã«ã¯ãããã»ã©å€ãã®è³ªåãè«äºãåŒãèµ·ããã¯ã©ã¹ã¯å€ããªããšæããŸãã
ãšããã§ã芪æãªãèªè
ã®çããããã®ã¯ã©ã¹ã«ã¯äœè¡ã®ã³ãŒãïŒã³ã¡ã³ãã¯å«ãŸãããã³ã¡ã³ããå«ãŸããŸããïŒããããŸããïŒ
質åãžã®çãã ãã£ãšé©ãã§ãããOptionalã¯ã©ã¹ã«ã¯ãã³ã¡ã³ããã«ãŠã³ãããªã100è¡ã®ã³ãŒãã®ã¿ãå«ãŸããŸãã ã³ã¡ã³ãã¯250è¡ã«ãªããŸãã
ãã®ã·ãªãŒãºã®èšäºã®ããã€ãã¯ãéåžžã«ã·ã£ãŒãã§èå³æ·±ãè°è«ãåŒãèµ·ãããŸããã ããããããã¯æµ·æŽã®äœäžã§ãããããŸããããJava 8ã®Optionalã®ç»å ŽåŸã«å¯å®¿åŠæ ¡ã®è±èªéšéã§ç»å Žããè°è«ã質åãšæ¯èŒããŠãäœäžååã§ããOptionalã«é¢ããæããããã§æãèå³æ·±ãè°è«ã®1ã€ã¯ãããã§ãïŒ
ïŒãã®è°è«ã«ã¯ãOracleã®ãJava Language Architectãã®å°äœãå ããBrian Goetzãåå ããŸããã Javaããã°ã©ããŒãã©ã®ããã«åœŒã®é è³ã䜿ã£ãŠããããèŠãŠãããã«åœŒããã©ãŒã©ã ã§æžãããã®ããããŸãïŒç§ã®ç¿»èš³ïŒïŒ
ãã¡ããã人ã
ã¯èªåãæãããšãããã§ãããã ãã ãããã®æ©èœãè¿œå ããéã«ã¯æ確ãªæå³ããããä»ã®èšèªã§MaybeãSomeãªã©ã®ã¡ã«ããºã ãäœæãããšããç®æšã¯èšå®ããŠããŸããã§ããã å€ãã®äººããããæãã§ãããããããŸãããã ç§ãã¡ã®ç®æšã¯ãã»ãšãã©ã®å Žåãšã©ãŒãåŒãèµ·ããå¯èœæ§ã®ãããŒãå€ã䜿çšãã代ããã«ããçµæãªããç¶æ³ãæ確ã«åŒ·èª¿ããŠãæ»ãå€ã®ã¿ã€ãã決å®ããã³ã³ãã¯ããªã¡ã«ããºã ãæäŸããããšã§ããã
ããšãã°ãããããçµæã®é
åãŸãã¯çµæã®ãªã¹ããè¿ããã®ã«ã¯äœ¿çšããªãã§ãã ããã 代ããã«ã空ã®é
åãŸãã¯ãªã¹ããè¿ããŸãã äœãã®ãã£ãŒã«ããã¡ãœããã®ãã©ã¡ãŒã¿ãŒãšããŠäœ¿çšããããšã¯ã»ãšãã©ãããŸããã
getterã®æ»ãå€ãšããŠã®éåžžã®äœ¿çšã¯ééããªãéå°ã«ãªããšæããŸãã
Optionalã¯é¿ããã¹ãæªãããšã§ã¯ãããŸããã ããã¯ãå€ãã®äººã代ããã«èŠãããã®ã§ã¯ãããŸããã ãããŠãç§ãã¡ã¯äœ¿ãããã®ãªã¹ã¯ãéåžžã«å¿é
ããŠããŸããã
åŠçïŒïŒãã®ã³ã³ããã¹ãã§ïŒåžžã«ãŒãã§ãªãããšã蚌æã§ããªãéããOptional.getãåŒã³åºããªãã§ãã ããã 代ããã«ãorElseãifPresentãªã©ã®å®å
šãªã¡ãœããã®ããããã䜿çšããŠãã ããã æ¯ãè¿ã£ãŠã¿ããšãgetã®ä»£ããã«getOrElseThrowNoSuchElementExceptionã®ãããªãã®ãæäŸããå¿
èŠãããããã®äœ¿çšãããæ確ã«ãªããŸãã ããããããã¯åŠãã æèšã§ãã
èšãæããã°ãèšèªã®äœæè
ã¯ãäžéšã®ïŒäžçŽïŒãŠãŒã¶ãŒãæãããã«ãèšèªãåžžã«æ¡åŒµãããšã¯éããŸããã ãŠãŒã¶ãŒã¯ãäœæè
ãèæ¡ããèšèªã®æ°ããæ©èœãåžžã«äœ¿çšãããšã¯éããŸããã ãŸããäœæè
ã¯ãä¿®æ£ã§ããªããšã©ãŒãåŸæããããšããããŸãã
ã ããã芪æãªãèªè
ã¯ãããªãèªèº«ã®ããã«èããããªãã®ãããžã§ã¯ãã§ãªãã·ã§ã³ã䜿çšããæ¹æ³ãèªåã§æ±ºããŠãã ããã ãããŠããã®äžé£ã®èšäºãšçŽæãããèŽãç©-Optionalã«é¢ãããã¹ã¿ãŒãããã«åœ¹ç«ã€ããšãé¡ãã°ããã§ãã
ãªãã·ã§ã³ã®ãã¹ã¿ãŒæºã®è¿ãã«ããå Žåã¯ã
ããªãã·ã§ã³ã®ãã¹ã¿ãŒããå°å·ããŠå£ã«æããããšããå§ãããŸãã ããã§ãªãå Žåã¯ãåã«ããŠã³ããŒãããŠãæã
調ã¹ãŠãã ããã
å·ŠåŽã®ã¯ã©ã¹ã®ã°ã«ãŒãã¡ãœããã®ååã«æ³šæããŠãã ããã é©åãªæ¹æ³ããã°ããèŠã€ããã®ã«åœ¹ç«ã¡ãŸãã
åã®äžã®9ã€ã¯ãã¡ãœãããJava 9以éã§äœ¿çšå¯èœã§ããããšã瀺ããŠããŸãã
æåŸã®åã¯ããã©ã¡ãŒã¿ãŒã®ã»ãã³ãã£ã¯ã¹ã匷調ããŠããŸãã
å
¥åãã©ã¡ãŒã¿ãŒã®ã¿ã€ãïŒå·ŠïŒãšçµæïŒå³ïŒã®ã»ãã³ãã£ã¯ã¹ã«ããããã©ã¡ãŒã¿ãŒããŒããŸãã¯éãŒãã®å Žåã«ã¡ãœãããäœããããããã°ããèŠããããšãã§ããŸãã
ããšãã°ãOptional.ofïŒïŒã¡ãœããã®å ŽåïŒ
(x, Ã)=>(O(x),Â¥)
éãã«ãã©ã¡ãŒã¿ãŒxã¯ãOïŒxïŒãšããŠãããããããªãã·ã§ã³ããããããããŸãã
nullå
¥åãã©ã¡ãŒã¿ãŒïŒÃã§ç€ºãããïŒã䜿çšããããšãããšãåèšå·ã§ç€ºãããäŸå€ãçºçããŸãïŒÂ¥ã