{"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 \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 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\u306e \u3053\u308c\u306b\u3088\u3063\u3066\u3001\u3044\u308f\u3086\u308b \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\u3001 monapt<\/a><\/p>\n TsMonad<\/a><\/p>\n
\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
\n
TypeScript<\/code>\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a
InnerFoo<\/code>\u304c
null<\/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\u3001
InnerFoo<\/code>\u3092
{ buzz: number } | null<\/code>\u3068\u8868\u73fe\u3057\u306a\u3044\u9650\u308a\u3001
const innerFoo = null;<\/code>\u306e\u3088\u3046\u306b
null<\/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\u3001
foo.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
Maybe\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\u305f
Maybe\u30e2\u30ca\u30c9<\/code>\u306b\u5305\u3093\u3067\u304d\u308c\u3044\u306b\u4f7f\u3044\u305f\u3044\u3082\u306e\u3067\u3059\u3002<\/p>\n
TypeScript<\/code>\u3067\u66f8\u304b\u308c\u305f
Maybe\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
Option<\/code>,
Try<\/code>,
Future<\/code>\u3092\u63d0\u4f9b\u3059\u308b\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u304c\u3061\u3087\u3063\u3068
Scala<\/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
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