{"id":309,"date":"2016-10-10T00:00:00","date_gmt":"2016-10-09T15:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/10\/10\/compare-maybe-monad"},"modified":"2016-10-10T00:00:00","modified_gmt":"2016-10-09T15:00:00","slug":"compare-maybe-monad","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/10\/10\/compare-maybe-monad\/","title":{"rendered":"TypeScript\u3067\u4f7f\u3048\u308bMaybe\u30e2\u30ca\u30c9\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u6bd4\u8f03"},"content":{"rendered":"

\u5c0f\u98fc\u3067\u3059\u3002
\nTypeScript<\/code>\u306e2\u7cfb\u304c\u6b63\u5f0f\u306b\u516c\u958b<\/a>\u3055\u308c\u307e\u3057\u305f\u3002
\n\u591a\u6570\u306e\u6a5f\u80fd\u8ffd\u52a0\u30fb\u6539\u5584\u304c\u3042\u308a\u307e\u3057\u305f\u304c\u3001\u4e2d\u3067\u3082\u76ee\u7389\u306e\u4e00\u3064\u306fstrictNullChecks<\/code>\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n

\u4eca\u307e\u3067\u66d6\u6627\u3060\u3063\u305f\u3001\u300e\u3042\u308b\u578b\u304cnull\/undefined\u3092\u53d6\u308a\u5f97\u308b\u304b\uff1f\u300f\u3068\u3044\u3046\u3053\u3068\u3092\u3001\u578b\u30ec\u30d9\u30eb\u3067\u53b3\u5bc6\u306b\u691c\u67fb\u30fb\u691c\u51fa\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n

\u5b9f\u884c\u6642\u30a8\u30e9\u30fc\u306b\u306a\u308b\u30d0\u30b0\u306e\u591a\u304f\u304c\u3001\u4e0d\u7528\u610f\u306aundefined\/null\u3078\u306e\u30a2\u30af\u30bb\u30b9\u304b\u3089\u751f\u3058\u308b\u3053\u3068\u304b\u3089\u3082\u3001\u3053\u308c\u304c\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5805\u7262\u6027\u306b\u5927\u304d\u304f\u5bc4\u4e0e\u3059\u308b\u6a5f\u80fd\u3067\u3042\u308b\u3053\u3068\u304c\u4f3a\u3048\u307e\u3059\u3002<\/p>\n

\n

JavaScript has two values for \u201cemptiness\u201d \u2013 null and undefined. If null is the billion dollar mistake, undefined only doubles our losses. These two values are a huge source of errors in the JavaScript world because users often forget to account for null or undefined being returned from APIs.<\/p>\n<\/blockquote>\n

\u4f8b\u3048\u3070\u5f93\u6765\u306eTypeScript<\/code>\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306aInnerFoo<\/code>\u304cnull<\/code>\u3092\u53d6\u308a\u5f97\u308b\u30b3\u30fc\u30c9\u3067\u3042\u3063\u3066\u3082\u3001\u554f\u984c\u306a\u304f\u30b3\u30f3\u30d1\u30a4\u30eb\u304c\u901a\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n

\/\/ (\u65e7)\u5b9f\u884c\u6642\u30a8\u30e9\u30fc\u306b\u306a\u308b\u304c\u3001Valid\u306a\u30b3\u30fc\u30c9\u3060\u3063\u305f\ninterface InnerFoo {\n  buzz: number;\n}\n\ninterface OuterFoo {\n  bar: InnerFoo;\n}\n\nconst innerFoo = null;\nconst foo: OuterFoo = { bar: innerFoo };\nconsole.log(foo.bar.buzz); \/\/ Uncaught TypeError: Cannot read property 'buzz' of null<\/code><\/pre>\n

strictNullChecks<\/code>\u3092\u6709\u52b9\u5316\u3057\u3066\u3044\u308b\u3068\u3001InnerFoo<\/code>\u3092{ buzz: number } | null<\/code>\u3068\u8868\u73fe\u3057\u306a\u3044\u9650\u308a\u3001const innerFoo = null;<\/code>\u306e\u3088\u3046\u306bnull<\/code>\u3092\u4ee3\u5165\u3067\u304d\u307e\u305b\u3093\u3002
\n\u307e\u305f\u3001InnerFoo<\/code>\u3092{ buzz: number } | null<\/code>\u3068\u8868\u73fe\u3059\u308b\u9650\u308a\u3001foo.bar.buzz<\/code>\u304b\u3089\u76f4\u63a5\u5024\u3092\u53d6\u308a\u51fa\u3059\u3053\u3068\u3082\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n

\/\/ (\u65b0)Foo.bar\u304cnull\u3092\u300e\u6301\u3061\u3046\u308b\u300f\u3068\u3044\u3046\u6587\u8108\u3092\u578b\u3068\u3057\u3066\u660e\u793a\u3057\u306a\u3044\u3068\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3088\u3046\u306b\u306a\u3063\u305f\ninterface InnerFoo {\n  buzz: number;\n}\n\ninterface OuterFoo {\n  bar: InnerFoo | null;\n}\n\nconst innerFoo = null;\nconst foo: OuterFoo = { bar: innerFoo };\n\nconst buzz: number = foo.bar ? foo.bar.buzz : -1;\nconsole.log(buzz);<\/code><\/pre>\n

\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u3044\u308f\u3086\u308bMaybe\u30e2\u30ca\u30c9(\u203b)<\/code>\u3067\u5305\u3093\u3067\u304a\u3044\u305f\u65b9\u304c\u826f\u3055\u305d\u3046\u306a\u578b\u304c\u691c\u51fa\u3055\u308c\u308b\u3001\u3068\u3082\u6349\u3048\u3089\u308c\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002
\n(Scala\u3068\u304bSwift\u3067\u306fOption\/Optional\u578b\u3068\u3057\u3066\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u306d)
\n\u305b\u3063\u304b\u304f\u3053\u306e\u6a5f\u80fd\u3092\u9069\u7528\u3059\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u751f\u306e\u6587\u6cd5\u901a\u308aT | null<\/code>\u3068\u66f8\u304f\u3088\u308a\u3082\u3001\u3044\u3044\u6a5f\u4f1a\u306a\u306e\u3067\u304d\u3061\u3093\u3068\u5b9a\u7fa9\u3055\u308c\u305fMaybe\u30e2\u30ca\u30c9<\/code>\u306b\u5305\u3093\u3067\u304d\u308c\u3044\u306b\u4f7f\u3044\u305f\u3044\u3082\u306e\u3067\u3059\u3002<\/p>\n

\u203b \u3059\u3054\u304f\u96d1\u306b\u8aac\u660e\u3059\u308b\u3068\u300e\u3042\u308b\u578b\u306e\u5024\u3092\u6301\u3063\u3066\u3044\u308b\u304b\u3082\u77e5\u308c\u306a\u3044\u300f\u578b\u306e\u3053\u3068<\/p>\n

\u305d\u3053\u3067\u672c\u7a3f\u3067\u306f\u3001TypeScript<\/code>\u3067\u66f8\u304b\u308c\u305fMaybe\u30e2\u30ca\u30c9<\/code>\u3092\u63d0\u4f9b\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u3044\u304f\u3064\u304b\u898b\u3066\u307f\u3066\u3001\u305d\u306e\u4f7f\u3044\u52dd\u624b\u3092\u691c\u8a3c\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

monapt<\/h2>\n

monapt<\/a><\/p>\n

Option<\/code>, Try<\/code>, Future<\/code>\u3092\u63d0\u4f9b\u3059\u308b\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u304c\u3061\u3087\u3063\u3068Scala<\/code>\u3063\u307d\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n

import { equal } from "assert";\nimport { Option, None } from "monapt";\n\ninterface InnerFoo {\n  buzz: number;\n}\n\ninterface OuterFoo {\n  bar: Option<InnerFoo>;\n}\n\nconst innerFoo: Option<InnerFoo> = None;\nconst foo: OuterFoo = { bar: innerFoo };\n\n\/\/ \u5024\u306e\u53d6\u308a\u51fa\u3057\nconst buzz1: number = foo.bar\n  .getOrElse(() => ({ buzz: -1 }))\n  .buzz;\n\n\/\/ \u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u98a8\u306e\u5024\u53d6\u308a\u51fa\u3057\nconst buzz2 = foo.bar\n  .match({\n    Some: x => x.buzz,\n    None: () => -1,\n  });\n\nequal(buzz1, -1);\nequal(buzz2, -1);<\/code><\/pre>\n

TsMonad<\/h2>\n

TsMonad<\/a><\/p>\n

Maybe<\/code>, Either<\/code>, Writer<\/code>\u3092\u63d0\u4f9b\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3001JavaScript\u306b\u304a\u3051\u308b\u4ee3\u6570\u7684\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5171\u901a\u4ed5\u69d8\u3092\u7b56\u5b9a\u3057\u3066\u3044\u308bFantasyLand<\/a>\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306b\u6e96\u62e0\u3057\u3066\u3044\u308b\u305d\u3046\u3067\u3059(\u3053\u306e\u8fba\u304b\u306a\u308a\u7406\u89e3\u304c\u602a\u3057\u3044\u306e\u3067\u3059\u304c...)<\/p>\n

\/\/\/ <reference path=".\/node_modules\/tsmonad\/dist\/tsmonad.d.ts" \/>\nimport { equal } from "assert";\nimport { Maybe } from "tsmonad";\n\ninterface InnerFoo {\n  buzz: number;\n}\n\ninterface OuterFoo {\n  bar: Maybe<InnerFoo>;\n}\n\nconst innerFoo: Maybe<InnerFoo> = Maybe.nothing<InnerFoo>();\nconst foo: OuterFoo = { bar: innerFoo };\n\n\/\/ \u5024\u306e\u53d6\u308a\u51fa\u3057\nconst buzz1 = foo.bar\n  .valueOr({ buzz: -1 })\n  .buzz;\n\n\/\/ \u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u98a8\u306e\u5024\u53d6\u308a\u51fa\u3057\nconst buzz2: number = foo.bar\n  .caseOf({\n    just: x => x.buzz,\n    nothing: () => -1,\n  });\n\nequal(buzz1, -1);\nequal(buzz2, -1);<\/code><\/pre>\n

monadness<\/h2>\n

monadness<\/a><\/p>\n

\u3053\u306e\u4e2d\u3067\u306f\u4e00\u756a\u82e5\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3001Either<\/code>, Option<\/code>, Tuples<\/code>\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002
\n\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306fmonapt<\/code>\u306b\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001\u552f\u4e00\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1(\u98a8)\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u88c5\u3055\u308c\u3066\u3044\u306a\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3082\u3042\u308a\u307e\u3059\u3002
\n\u307e\u3060\u958b\u767a\u4e2d\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u3088\u3046\u3067\u3059\u306e\u3067\u3001\u4eca\u5f8c\u5b9f\u88c5\u3055\u308c\u3066\u3044\u304f\u4e88\u5b9a\u306a\u306e\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u3002<\/p>\n

import { equal } from "assert";\nimport { Option } from "monadness";\n\ninterface InnerFoo {\n  buzz: number;\n}\n\ninterface OuterFoo {\n  bar: Option<InnerFoo>;\n}\n\nconst innerFoo: Option<InnerFoo> = Option.none<InnerFoo>();\nconst foo: OuterFoo = { bar: innerFoo };\n\n\/\/ \u5024\u306e\u53d6\u308a\u51fa\u3057\nconst buzz1 = foo.bar\n  .getOrElse(() => ({ buzz: -1 }))\n  .buzz;\n\nequal(buzz1, -1);<\/code><\/pre>\n

\u307e\u3068\u3081<\/h2>\n

TypeScript<\/code>\u3067\u6271\u3048\u308bMaybe\u30e2\u30ca\u30c9<\/code>\u3092\u63d0\u4f9b\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u3044\u304f\u3064\u304b\u7d39\u4ecb\u3057\u3066\u307f\u307e\u3057\u305f\u3002
\nJavaScript<\/code>\u306b\u306f\u8a00\u8a9e\u7d44\u307f\u8fbc\u307f\u306e\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u304c\u7121\u3044\u3060\u3051\u306b\u3001\u4ee3\u66ff\u3068\u306a\u308b\u30e1\u30bd\u30c3\u30c9\u3092\u751f\u3084\u3057\u3066\u30d7\u30ed\u30d1\u30c6\u30a3\u540d\u3067\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u98a8\u306e\u52d5\u4f5c\u3092\u5b9f\u73fe\u3057\u3066\u3044\u308b\u3088\u3046\u306a\u5b9f\u88c5\u306b\u30b0\u30c3\u3068\u6765\u307e\u3057\u305f\u3002
\n(\u5f53\u7136\u7d30\u304b\u3044\u30d1\u30bf\u30fc\u30f3\u306e\u6307\u5b9a\u3084\u30d1\u30bf\u30fc\u30f3\u30ac\u30fc\u30c9\u306f\u3067\u304d\u306a\u3044\u306e\u3067\u3001Scala<\/code>\u3084Swift<\/code>\u306e\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u3068\u8f03\u3079\u3066\u3057\u307e\u3046\u3068\u6a5f\u80fd\u7684\u306b\u304b\u306a\u308a\u898b\u52a3\u308a\u3057\u3066\u3057\u307e\u3046\u306e\u3067\u3059\u304c...)<\/p>\n

\u3055\u3066\u3001\u5b9f\u969b\u306b\u7c21\u5358\u306a\u30b3\u30fc\u30c9\u3092\u63cf\u3044\u3066\u307f\u3066Maybe<\/code>\u306e\u307f\u306e\u4f7f\u3044\u52dd\u624b\u3067\u8a00\u3046\u3068\u3001\u3069\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u3082\u5927\u304d\u306a\u5dee\u306f\u7121\u3044\u3088\u3046\u306b\u601d\u3044\u307e\u3059\u3002
\n\u3053\u306e\u4e2d\u304b\u3089\u9078\u3076\u3068\u3059\u308b\u3068\u3001\u540c\u6642\u306b\u63d0\u4f9b\u3057\u3066\u3044\u308b\u578b\u306b\u6b32\u3057\u3044\u3082\u306e\u304c\u3042\u308b\u304b\u3001\u3067\u9078\u3076\u3053\u3068\u306b\u306a\u308b\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u3002<\/p>\n

\u500b\u4eba\u7684\u306b\u306f\u3001Maybe<\/code>\u30e2\u30ca\u30c9\u306b\u4f59\u5206\u306a(filter\u3068\u304b)\u30e1\u30bd\u30c3\u30c9\u304c\u751f\u3048\u3066\u3066\u4fbf\u5229\u305d\u3046\u306a\u306e\u3068\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u304c\u6700\u8fd1\u304b\u3058\u3063\u3066\u3044\u308bScala<\/code>\u306b\u4f3c\u3066\u3044\u3066\u8aad\u307f\u3084\u3059\u3044\u3042\u305f\u308a\u3067monapt<\/code>\u3092\u9078\u3073\u305f\u3044\u3068\u601d\u3063\u3066\u3044\u307e\u3059\u3002(Either<\/code>\u304c\u6b32\u3057\u3044\u3068\u3053\u308d\u3067\u3059\u304c\u3001Try<\/code>\u304c\u305d\u308c\u306b\u76f8\u5f53\u3059\u308b\u578b\u3068\u3057\u3066\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u306e\u3067)<\/p>\n

\u7c21\u5358\u306a\u5185\u5bb9\u3067\u3057\u305f\u304c\u3001\u4eca\u56de\u66f8\u3044\u305f\u30b3\u30fc\u30c9\u306f\u3053\u3053\u306b\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3057\u305f
\n
compare-maybe-monad<\/a><\/p>\n

\u4ee5\u4e0a\u3001\u53c2\u8003\u306b\u306a\u308c\u3070\u3046\u308c\u3057\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5c0f\u98fc\u3067\u3059\u3002 TypeScript\u306e2\u7cfb\u304c\u6b63\u5f0f\u306b\u516c\u958b\u3055\u308c\u307e\u3057\u305f\u3002 \u591a\u6570\u306e\u6a5f\u80fd\u8ffd\u52a0\u30fb\u6539\u5584\u304c\u3042\u308a\u307e\u3057\u305f\u304c\u3001\u4e2d\u3067\u3082\u76ee\u7389\u306e\u4e00\u3064\u306fstrictNullChecks\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002 \u4eca\u307e\u3067\u66d6\u6627\u3060\u3063\u305f\u3001\u300e\u3042\u308b\u578b\u304cnull\/undefined\u3092\u53d6\u308a\u5f97\u308b\u304b\uff1f\u300f\u3068\u3044\u3046\u3053\u3068\u3092\u3001\u578b\u30ec\u30d9\u30eb\u3067\u53b3\u5bc6\u306b\u691c\u67fb\u30fb\u691c\u51fa\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u6a5f\u80fd\u3067\u3059\u3002 \u5b9f\u884c\u6642\u30a8\u30e9\u30fc\u306b\u306a\u308b\u30d0\u30b0\u306e\u591a\u304f\u304c\u3001\u4e0d\u7528\u610f\u306aundefined\/null\u3078\u306e\u30a2\u30af\u30bb […]<\/p>\n","protected":false},"author":1,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[77,174],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/309"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}