{"id":216838,"date":"2021-05-01T08:17:03","date_gmt":"2021-05-01T00:17:03","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=216838"},"modified":"2021-04-25T09:18:56","modified_gmt":"2021-04-25T01:18:56","slug":"understand-the-use","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/understand-the-use.html","title":{"rendered":"\u4e86\u89e3\u4e0bScala \u4f7f\u7528 Option\u3001Some\u3001None\uff0c\u907f\u514d null \u4f7f\u7528"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5927\u591a\u6570\u8bed\u8a00\u90fd\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5173\u952e\u5b57\u6216\u8005\u5bf9\u8c61\u6765\u8868\u793a\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\u7684\u662f\"\u65e0\"\uff0c\u5728 Java\uff0c\u5b83\u662f null\u3002\u5728 Java \u91cc\uff0cnull \u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5bf9\u5b83\u8c03\u7528\u4efb\u4f55\u65b9\u6cd5\u90fd\u662f\u975e\u6cd5\u7684\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u907f\u514d null \u4f7f\u7528<\/strong><\/div>\n

\u5927\u591a\u6570\u8bed\u8a00\u90fd\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5173\u952e\u5b57\u6216\u8005\u5bf9\u8c61\u6765\u8868\u793a\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\u7684\u662f\"\u65e0\"\uff0c\u5728 Java\uff0c\u5b83\u662f null\u3002\u5728 Java \u91cc\uff0cnull \u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5bf9\u5b83\u8c03\u7528\u4efb\u4f55\u65b9\u6cd5\u90fd\u662f\u975e\u6cd5\u7684\u3002\u4f46\u662f\u8fd9\u5bf9\u8bed\u8a00\u8bbe\u8ba1\u8005\u6765\u8bf4\u662f\u4e00\u4ef6\u4ee4\u4eba\u7591\u60d1\u7684\u9009\u62e9\u3002\u4e3a\u4ec0\u4e48\u8981\u5728\u7a0b\u5e8f\u5458\u5e0c\u671b\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019\u8fd4\u56de\u4e00\u4e2a\u5173\u952e\u5b57\u5462\uff1f<\/p>\n

Scala \u7684 Option\u7c7b\u578b<\/strong><\/div>\n

\u4e3a\u4e86\u8ba9\u6240\u6709\u4e1c\u897f\u90fd\u662f\u5bf9\u8c61\u7684\u76ee\u6807\u66f4\u52a0\u4e00\u81f4\uff0c\u4e5f\u4e3a\u4e86\u9075\u5faa\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e60\u60ef\uff0cScala \u9f13\u52b1\u4f60\u5728\u53d8\u91cf\u548c\u51fd\u6570\u8fd4\u56de\u503c\u53ef\u80fd\u4e0d\u4f1a\u5f15\u7528\u4efb\u4f55\u503c\u7684\u65f6\u5019\u4f7f\u7528 Option \u7c7b\u578b\u3002\u5728\u6ca1\u6709\u503c\u7684\u65f6\u5019\uff0c\u4f7f\u7528 None\uff0c\u8fd9\u662f Option \u7684\u4e00\u4e2a\u5b50\u7c7b\u3002\u5982\u679c\u6709\u503c\u53ef\u4ee5\u5f15\u7528\uff0c\u5c31\u4f7f\u7528 Some \u6765\u5305\u542b\u8fd9\u4e2a\u503c\u3002Some \u4e5f\u662f Option \u7684\u5b50\u7c7b\u3002 None \u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u7c7b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u8981\u5b83\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u6837\uff0c\u5b83\u591a\u5c11\u6709\u70b9\u50cf null \u5173\u952e\u5b57\uff0c\u4f46\u5b83\u5374\u662f\u4e00\u4e2a\u5b9e\u5b9e\u5728\u5728\u7684\uff0c\u6709\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/p>\n

\u5e94\u7528\u4f8b\u5b50<\/strong><\/div>\n

Option \u7c7b\u578b\u7684\u503c\u901a\u5e38\u4f5c\u4e3a Scala \u96c6\u5408\u7c7b\u578b\uff08List, Map \u7b49\uff09\u64cd\u4f5c\u7684\u8fd4\u56de\u7c7b\u578b\u3002\u6bd4\u5982 Map \u7684 get \u65b9\u6cd5\uff1a<\/p>\n

scala> val capitals = Map(\"France\"->\"Paris\", \"Japan\"->\"Tokyo\", \"China\"->\"Beijing\")\r\ncapitals: scala.collection.immutable.Map[String,String] = Map(France -> Paris, Japan -> Tokyo, China -> Beijing)\r\n\r\nscala> capitals get \"France\"\r\nres0: Option[String] = Some(Paris)\r\n\r\nscala> capitals get \"North Pole\"\r\nres1: Option[String] = None<\/pre>\n

Option \u6709\u4e24\u4e2a\u5b50\u7c7b\u522b\uff0cSome \u548c None\u3002\u5f53\u7a0b\u5e8f\u56de\u4f20 Some \u7684\u65f6\u5019\uff0c\u4ee3\u8868\u8fd9\u4e2a\u51fd\u5f0f\u6210\u529f\u5730\u7ed9\u4e86\u4f60\u4e00\u4e2a String\uff0c\u800c\u4f60\u53ef\u4ee5\u900f\u8fc7 get() \u51fd\u6570\u62ff\u5230\u90a3\u4e2a String\uff0c\u5982\u679c\u7a0b\u5e8f\u8fd4\u56de\u7684\u662f None\uff0c\u5219\u4ee3\u8868\u6ca1\u6709\u5b57\u7b26\u4e32\u53ef\u4ee5\u7ed9\u4f60\u3002<\/p>\n

\u5728\u8fd4\u56de None\uff0c\u4e5f\u5c31\u662f\u6ca1\u6709 String \u7ed9\u4f60\u7684\u65f6\u5019\uff0c\u5982\u679c\u4f60\u8fd8\u786c\u8981\u8c03\u7528 get() \u6765\u53d6\u5f97 String \u7684\u8bdd\uff0cScala \u4e00\u6837\u662f\u4f1a\u629b\u51fa\u4e00\u4e2a NoSuchElementException \u5f02\u5e38\u7ed9\u4f60\u7684\u3002 \u6211\u4eec\u4e5f\u53ef\u4ee5\u9009\u7528\u53e6\u5916\u4e00\u4e2a\u65b9\u6cd5 getOrElse\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728\u8fd9\u4e2a Option \u662f Some \u7684\u5b9e\u4f8b\u65f6\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff0c\u800c\u5728\u662f None \u7684\u5b9e\u4f8b\u65f6\u8fd4\u56de\u4f20\u5165\u7684\u53c2\u6570\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f20\u5165 getOrElse \u7684\u53c2\u6570\u5b9e\u9645\u4e0a\u662f\u9ed8\u8ba4\u8fd4\u56de\u503c\u3002<\/p>\n

scala> capitals get \"North Pole\" get\r\nwarning: there was one feature warning; re-run with -feature for details\r\njava.util.NoSuchElementException: None.get\r\n  at scala.None$.get(Option.scala:347)\r\n  at scala.None$.get(Option.scala:345)\r\n  ... 33 elided\r\n\r\nscala> capitals get \"France\" get\r\nwarning: there was one feature warning; re-run with -feature for details\r\nres3: String = Paris\r\n\r\nscala> (capitals get \"North Pole\") getOrElse \"Oops\"\r\nres7: String = Oops\r\n\r\nscala> capitals get \"France\" getOrElse \"Oops\"\r\nres8: String = Paris<\/pre>\n

\u901a\u8fc7\u6a21\u5f0f\u5339\u914d\u5206\u79bb\u53ef\u9009\u503c\uff0c\u5982\u679c\u5339\u914d\u7684\u503c\u662f Some \u7684\u8bdd\uff0c\u5c06 Some \u91cc\u7684\u503c\u62bd\u51fa\u8d4b\u7ed9 x \u53d8\u91cf\uff1a<\/p>\n

def showCapital(x: Option[String]) = x match {\r\n    case Some(s) => s\r\n    case None => \"?\"\r\n}<\/pre>\n
\u63d0\u793a<\/strong><\/div>\n

Scala \u7a0b\u5e8f\u4f7f\u7528 Option \u975e\u5e38\u9891\u7e41\uff0c\u5728 Java \u4e2d\u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u503c\uff0c\u4ee3\u7801\u4e2d\u5f88\u591a\u5730\u65b9\u90fd\u8981\u6dfb\u52a0 null \u5173\u952e\u5b57\u68c0\u6d4b\uff0c\u4e0d\u7136\u5f88\u5bb9\u6613\u51fa\u73b0 NullPointException\u3002\u56e0\u6b64 Java \u7a0b\u5e8f\u9700\u8981\u5173\u5fc3\u90a3\u4e9b\u53d8\u91cf\u53ef\u80fd\u662f null,\u800c\u8fd9\u4e9b\u53d8\u91cf\u51fa\u73b0 null \u7684\u53ef\u80fd\u6027\u5f88\u4f4e\uff0c\u4f46\u4e00\u4f46\u51fa\u73b0\uff0c\u5f88\u96be\u67e5\u51fa\u4e3a\u4ec0\u4e48\u51fa\u73b0 NullPointerException\u3002 Scala \u7684 Option \u7c7b\u578b\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u56e0\u6b64 Scala \u5e94\u7528\u63a8\u8350\u4f7f\u7528 Option \u7c7b\u578b\u6765\u4ee3\u8868\u4e00\u4e9b\u53ef\u9009\u503c\u3002\u4f7f\u7528 Option \u7c7b\u578b\uff0c\u8bfb\u8005\u4e00\u773c\u5c31\u53ef\u4ee5\u770b\u51fa\u8fd9\u79cd\u7c7b\u578b\u7684\u503c\u53ef\u80fd\u4e3a None\u3002<\/p>\n

\u5b9e\u9645\u4e0a\uff0c\u591a\u4e8f Scala \u7684\u9759\u6001\u7c7b\u578b\uff0c\u4f60\u5e76\u4e0d\u80fd\u9519\u8bef\u5730\u5c1d\u8bd5\u5728\u4e00\u4e2a\u53ef\u80fd\u4e3a null \u7684\u503c\u4e0a\u8c03\u7528\u65b9\u6cd5\u3002\u867d\u7136\u5728 Java \u4e2d\u8fd9\u662f\u4e2a\u5f88\u5bb9\u6613\u72af\u7684\u9519\u8bef\uff0c\u5b83\u5728 Scala \u5374\u901a\u4e0d\u8fc7\u7f16\u8bd1\uff0c\u8fd9\u662f\u56e0\u4e3a Java \u4e2d\u6ca1\u6709\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u4e3a null \u7684\u7f16\u7a0b\u4f5c\u4e3a\u53d8\u6210 Scala \u4e2d\u7684\u7c7b\u578b\u9519\u8bef\uff08\u4e0d\u80fd\u5c06 Option[String] \u5f53\u505a String \u6765\u4f7f\u7528\uff09\u3002\u6240\u4ee5\uff0cOption \u7684\u4f7f\u7528\u6781\u5f3a\u5730\u9f13\u52b1\u4e86\u66f4\u52a0\u5f39\u6027\u7684\u7f16\u7a0b\u4e60\u60ef\u3002<\/p>\n

\u8be6\u89e3 Option[T]<\/strong><\/div>\n

\u5728 Scala \u91cc Option[T] \u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5bb9\u5668\uff0c\u5c31\u50cf\u6570\u7ec4\u6216\u662f List \u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u628a\u4ed6\u770b\u6210\u662f\u4e00\u4e2a\u53ef\u80fd\u6709\u96f6\u5230\u4e00\u4e2a\u5143\u7d20\u7684 List\u3002<\/p>\n

\u5f53\u4f60\u7684 Option \u91cc\u9762\u6709\u4e1c\u897f\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a List \u7684\u957f\u5ea6\u662f 1\uff08\u4e5f\u5c31\u662f Some\uff09\uff0c\u800c\u5f53\u4f60\u7684 Option \u91cc\u6ca1\u6709\u4e1c\u897f\u7684\u65f6\u5019\uff0c\u5b83\u7684\u957f\u5ea6\u662f 0\uff08\u4e5f\u5c31\u662f None\uff09\u3002<\/p>\n

for \u5faa\u73af<\/strong><\/div>\n

\u5982\u679c\u6211\u4eec\u628a Option \u5f53\u6210\u4e00\u822c\u7684 List \u6765\u7528\uff0c\u5e76\u4e14\u7528\u4e00\u4e2a for \u5faa\u73af\u6765\u8d70\u8bbf\u8fd9\u4e2a Option \u7684\u65f6\u5019\uff0c\u5982\u679c Option \u662f None\uff0c\u90a3\u8fd9\u4e2a for \u5faa\u73af\u91cc\u7684\u7a0b\u5e8f\u4ee3\u7801\u81ea\u7136\u4e0d\u4f1a\u6267\u884c\uff0c\u4e8e\u662f\u6211\u4eec\u5c31\u8fbe\u5230\u4e86\u300c\u4e0d\u7528\u68c0\u67e5 Option \u662f\u5426\u4e3a None \u8fd9\u4ef6\u4e8b\u3002<\/p>\n

scala> val map1 = Map(\"key1\" -> \"value1\")\r\nmap1: scala.collection.immutable.Map[String,String] = Map(key1 -> value1)\r\n\r\nscala> val value1 = map1.get(\"key1\")\r\nvalue1: Option[String] = Some(value1)\r\n\r\nscala> val value2 = map1.get(\"key2\")\r\nvalue2: Option[String] = None\r\n\r\nscala> def printContentLength(x: Option[String]) {\r\n     |   for (c <- x){\r\n     |     println(c.length)\r\n     |   }\r\n     | }\r\nprintContentLength: (x: Option[String])Unit\r\n\r\nscala> printContentLength(value1)\r\n6\r\n\r\nscala> printContentLength(value2)<\/pre>\n
map \u64cd\u4f5c<\/strong><\/div>\n

\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u6709\u4e00\u4e2a\u6838\u5fc3\u7684\u6982\u5ff5\u4e4b\u4e00\u662f\u8f6c\u6362\uff0c\u6240\u4ee5\u5927\u90e8\u4efd\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u90fd\u652f\u6301\u4e00\u79cd\u53eb map() \u7684\u52a8\u4f5c\uff0c\u8fd9\u4e2a\u52a8\u4f5c\u662f\u53ef\u4ee5\u5e2e\u4f60\u628a\u67d0\u4e2a\u5bb9\u5668\u7684\u5185\u5bb9\uff0c\u5957\u4e0a\u4e00\u4e9b\u52a8\u4f5c\u4e4b\u540e\uff0c\u53d8\u6210\u53e6\u4e00\u4e2a\u65b0\u7684\u5bb9\u5668\u3002<\/p>\n

\u73b0\u5728\u6211\u4eec\u8003\u8651\u5982\u4f55\u7528 Option \u7684 map \u65b9\u6cd5\u5b9e\u73b0 length: xxx \u7684\u8f93\u51fa\u5f62\u5f0f\uff1a<\/p>\n

\u5148\u7b97\u51fa Option \u5bb9\u5668\u5185\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<\/p>\n

\u7136\u540e\u5728\u957f\u5ea6\u524d\u9762\u52a0\u4e0a \"length: \" \u5b57\u6837<\/p>\n

\u6700\u540e\u628a\u5bb9\u5668\u8d70\u8bbf\u4e00\u6b21\uff0c\u5370\u51fa\u5bb9\u5668\u5185\u7684\u4e1c\u897f<\/p>\n

scala> value1.map(_.length).map(\"length: \" + _).foreach(println)\r\nlength: 6\r\n\r\nscala> value1.map(\"length: \" + _.length).foreach(println)\r\nlength: 6<\/pre>\n

\u900f\u8fc7\u8fd9\u6837\u300c\u8f6c\u6362\u300d\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u4e00\u6837\u53ef\u4ee5\u8fbe\u6210\u60f3\u8981\u7684\u6548\u679c\uff0c\u800c\u4e14\u540c\u6837\u4e0d\u7528\u53bb\u505a\u300c\u662f\u5426\u4e3a None\u300d\u7684\u5224\u65ad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5927\u591a\u6570\u8bed\u8a00\u90fd\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5173\u952e\u5b57\u6216\u8005\u5bf9\u8c61\u6765\u8868\u793a\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\u7684\u662f”\u65e0”\uff0c\u5728 Java\uff0c\u5b83\u662f null\u3002\u5728 Java […]<\/p>\n","protected":false},"author":1481,"featured_media":216840,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-216838","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=216838"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216838\/revisions"}],"predecessor-version":[{"id":216842,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216838\/revisions\/216842"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/216840"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=216838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=216838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=216838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}