Erlangã§ã¯ããæ»ã¬ãã¨ï¼ããã»ã¹ãã¯ã©ãã·ã¥ããããã¨ãã®è§£éï¼æ義ï¼ä¾¡å¤è¦³ããä»ã®è¨èªã¨ã¯éåéãã¾ããæ½ï¼ããããï¼ãæ»ã¬ãã¨ãæ¨å¥¨ããã¦ãã¾ãããããã¯è²¬åã®æ¾æ£ã¨ã¯éãã¾ãã
å 容ï¼
- äºæ ãç½å®³ã¸ã®å¯¾å¦ã¯å人ã§ã¯ãªãã¦ä¼æ¥ã社ä¼ãè¡ãã¹ã
- æ£å¸¸ã¨ç°å¸¸ã®ã¯ãã¾
- ä¾å¤ã使ãã®ã¯ä¾å¤çï¼
- å¤ããã»ã¹ä¸¦åããã°ã©ãã³ã°ã¨ä¾å¤
- æ½ãã¨ç¡è²¬ä»»ã¯éã -- æ¦å£«éããã°ã©ãã³ã°
âäºæ ãç½å®³ã¸ã®å¯¾å¦ã¯å人ã§ã¯ãªãã¦ä¼æ¥ã社ä¼ãè¡ãã¹ã
Erlangã®æ¸ãæ¹ãæåã§ããªããªã馴æããªãã®ããç°å¸¸æã®å¦çãæ¸ããªããã¨ããæ¹éã§ãã
å¤ãã®ããã°ã©ãã³ã°è¨èªã®ã³ã¼ãã§ã¯ã次ã®ãããªåå²ããã°ãã°è¦åãã¾ãã
if (æ£å¸¸æ¡ä»¶) {
æ£å¸¸æã®å¦ç;
} else {
ç°å¸¸æã®å¦ç;
}switch (å¤) {
case æ£å¸¸ãªå¤_1 :
æ£å¸¸æã®å¦ç_1; break;
case æ£å¸¸ãªå¤_2 :
æ£å¸¸æã®å¦ç_2; break;
// ...
default :
ç°å¸¸æã®å¦ç
}
Erlangã§ã¯ãåºæ¬çã«ç°å¸¸æã®å¦çã¯æ¸ãã¾ããã
if
æ£å¸¸æ¡ä»¶ ->
æ£å¸¸æã®å¦ç
endcase å¤ of
æ£å¸¸ãªå¤_1 ->
æ£å¸¸æã®å¦ç_1;
æ£å¸¸ãªå¤_2 ->
æ£å¸¸æã®å¦ç_2;
%% ...
æ£å¸¸ãªå¤_n ->
æ£å¸¸æã®å¦ç_n
end
æ£å¸¸æ¡ä»¶ï¼æ£å¸¸å¤ããã¯ãããäºæ ãèµ·ããã¨ãèªåçã«ã©ã³ã¿ã¤ã ã¨ã©ã¼ãçºçãã¦ããã»ã¹ãæ»ã«ã¾ããå¤ãã®ããã°ã©ãã³ã°è¨èªï¼ã©ã³ã¿ã¤ã ã·ã¹ãã ã§ã¯ãããã»ã¹ãæ»ã¬ã®ã¯å¤§æ¨äºã§ãããErlangã®è»½éããã»ã¹ãæ»ãã§ãããããé¨ãã§ã¯ããã¾ãããè¤æ°ããã»ã¹ã®å調äºå©ä½å¶ãä½ã£ã¦ããã°ãããã¤ãã®ããã»ã¹ãæ»ãã§ãã·ã¹ãã å ¨ä½ã¨ãã¦ã¯æ£å¸¸åä½ãä¿è¨¼ã§ãã¾ã*1ã
ã¤ã¾ããç°å¸¸äºæ ã¸ã®å¯¾å¦ãå復å¦çãåã ã®é¢æ°ãããã»ã¹ã§ã¯è¡ããããã£ã¨ãã¯ããªçµç¹æ§é ï¼ç¤¾ä¼ä½å¶ã®ãããªãã®ã§ã»ã¼ãããããæ§ç¯ããã®ã§ããã¢ã¼ã ã¹ããã³ã°ã®è¨èãå¼ç¨ãã¦ããã¨ï¼
Joe Armstrong, "defensive programming"
http://www.erlang.org/pipermail/erlang-questions/2003-March/007869.html
If you do *nothing* to your code you get a good diagnostic anyway:
ã³ã¼ãã«ä½ãæ¸ããªããã°ãããè¯ã診æã¡ãã»ã¼ã¸ãå¾ãããã[æªå±±è£è¶³:ä½è¨ãªãã¨ãæ¸ãã¨ãã¯ãªãã¨ã¯ãªããããæ¢ããã]
In C etc. you have to write *something* if you detect an error - in Erlang it's easy - don't even bother to write code that checks for errors - "just let it crash".
Cã¨ãã ã¨ãã¨ã©ã¼ãæ¤åºãããä½ãæ¸ããªããã°ãªããªããããErlangã§ã¯ããã·ãç°¡åãã¨ã©ã¼ã®æ¤æ»ã対å¦ã®ã³ã¼ããããããæ¸ãããã¯ããªãããã¾ããªããããããã»ã¹ãã¯ã©ãã·ã¥ãããã°ããã®ãã
âæ£å¸¸ã¨ç°å¸¸ã®ã¯ãã¾
ã¨ããããã§ãå¼æ°ãã¡ãã»ã¼ã¸ãå ¥åã¹ããªã¼ã ãªã©ããæ³å®å¤ã®ãã¼ã¿ãåãåã£ãã¨ãã¯ãä½ãããªãã§ã¢ããµãªæ»ã¬ã®ãErlangæµãªã®ã§ããæ½ãæ»ã¬ã³ã¼ããæ¸ãã«ã¯ãç°å¸¸äºæ ãªãã¦èµ·ãããªãã¨ä»®å®ãã¦ãæ£å¸¸å¦çã ãããã©ãã©æ¸ãã°ããã®ã§ãã
ãããå®éã«ã¯ããæ³å®å ã®ã¨ã©ã¼ããããã¾ããä¾ãã°ããªãããã®æ§æã®ãã¼ã¶ã¼ãæ¸ãã¨ãã¾ããå ¥åãã¡ã¤ã«ã®æ§æã¨ã©ã¼ãè¦ã¤ãã£ããæ»ãã§ãã¾ãã®ã§ã¯ä½¿ããã®ã«ãªãã¾ãããæ¼¢ï¼ãã¨ãï¼ããéãã¦ãã«ã§ãããã®å ´åãå¼ã³å´ã«æ§æã¨ã©ã¼æ å ±ãä¼ããªãã¦ã¯ãªãã¾ããã
ã§ã¯ããã¼ãºãã¹ããã¡ã¤ã«ãéããªãã¨ãã¯ã©ãã§ãããï¼ ããã¯ç°å¸¸äºæ ã¨èãããã¨ãã§ãã¾ããããããããä¸é½åãï¼æ»ã¬ããããªãï¼ç¨åº¦ã«èãã¦ãå¼ã³å´ã«ä¼ããã¨ããå¤æãããã§ãããã
æ¢åã®Erlangããã°ã©ã ã®å¤ãã§ã¯ã次ã®ãããªæ¹éã§ã¨ã©ã¼ã¸ã®å¯¾å¦ã¨ã¨ã©ã¼æ å ±ã®ä¼éããã¦ãã¾ãã
- æ³å®å¤ã®ç°å¸¸äºæ ã¯ã©ã³ã¿ã¤ã ã·ã¹ãã ã«ä»»ããï¼ä½ãæ¸ããªãï¼ã
- ãã¾ãã¾ãæ³å®å¤ã®ç°å¸¸äºæ ãèªåã§æ¤åºãã¦ãã¾ã£ãããerlang:error/1ï¼æ§ç§°ã¯ erlang:fault/1 å»æ¢äºå®ï¼ã§ããã«ã¯ã©ãã·ã¥ãã*2ã
- æ³å®å ã®ã¨ã©ã¼ã¯ãããªã¢ã³ãåã®æ»ãå¤ã§ä¼ããã
æå¾ã®ããªã¢ã³ãåã¨ã¯ãæ£å¸¸å¤ã¯ {ok, Value}ãã¨ã©ã¼å¤ã¯ {error, Reason} ã¨ãããã¿ã¼ã³ã§ãããErlangå®é¨å®¤ï¼ä¾å¤çå¤ã¨ããªã¢ã³ãåãã¼ã¿ããåç §ãã¦ãã ããã
âä¾å¤ã使ãã®ã¯ä¾å¤çï¼
ããããã¯ãç°å¸¸äºæ ï¼ä¸æ ®ã®äºæ ãç½å®³ï¼ã§ã¯ãªãã¦ãæ³å®å ã®ã¨ã©ã¼ï¼äºæ¸¬ãããè¥å¹²ã®ä¸é½åï¼ã«ã¤ãã¦èãã¾ããåç¯ã§è¿°ã¹ããã¨ããæ¨æºã©ã¤ãã©ãªãå«ãã{ok, Value} ã¾ã㯠{error, Reason} ãæ»ãæ¹å¼ï¼ä»¥ä¸ãok/erroræ¹å¼ã¨å¼ã³ã¾ãï¼ãå§åçã«å¤ã使ããã¦ãã¾ãã
ããããã«ã¼ã«ã½ã³ï¼Richard Carlssonï¼ã¯ãok/erroræ¹å¼ã®å¼å®³ãææãã¦ãã¾ãã
- http://www.erlang.se/workshop/2004/exception.pdf è«æ
- http://www.erlang.se/euc/04/carlsson_slides.pdf ã¹ã©ã¤ã
ã«ã¼ã«ã½ã³ã®è¨ããã¨ãæ¤è¨ãã¦ã¿ãã¨ãå®ã«ãã®ã¨ããã§ãæ³å®å ã®ã¨ã©ã¼ã¯ok/erroræ¹å¼ã§ã¯ãªãã¦ãthrowã§æããã¹ããªã®ã§ããã«ãããããããæ¨æºã©ã¤ãã©ãªã§ããok/erroræ¹å¼ãªã®ã¯ãªãã§ãããï¼ ãªãã¨ãErlangã®ãªãªã¼ã¹10ï¼ææ°çã¯ãªãªã¼ã¹12ãR12B-*ï¼ã«ãªãã¾ã§ãã¾ã¨ããªä¾å¤æ©æ§ãåå¨ããªãã£ãã®ã§ãã
æããcatchå¼ã¯ãã£ãã®ã§ãããããã¯Lispã®catchã«ç±æ¥ãããã®ã§ãããããä¾å¤ã¨ããããã¯ãé¢æ°ãè¶ ãã¦ã®gotoã§ããã大åè±åºå¶å¾¡ç¨ã®ãã¼ã«ã§ãããããã£ã¦ãæ³å®å ã®ã¨ã©ã¼ã®ä¼éã«throwã使ãã¨ããçºæ³ã«ã¯ãªããªãã£ãããã§ãã
ä»å¾ã®Erlangããã°ã©ãã³ã°ã§ã¯ãã¾ã¨ããªä¾å¤æ©æ§ãã¤ã¾ãtryå¼ã使ãã¹ãã§ãããã1åã®ããã»ã¹å ã§èµ°ãé次ããã°ã©ãã³ã°ã«é¢ãã¯ãä¾å¤ã使ãã°ã³ã¼ããæ¹åããã¾ãã
âå¤ããã»ã¹ä¸¦åããã°ã©ãã³ã°ã¨ä¾å¤
é次ããã°ã©ãã³ã°ã«ãããä¾å¤ã®ä½¿ç¨æ³ãããã¼ã¯ãä»ã®è¨èªã¨å¤ãããªãã¨æãã¾ããåé¡ã¯ãè¤æ°ã®ããã»ã¹ã使ã並åããã°ã©ãã³ã°ã§ããããã»ã¹éã§ç°å¸¸äºæ ãéç¥ããæ¹æ³ã«ã·ã°ãã«ãããã¾ãããããã¯ãæ³å®å ã®ã¨ã©ã¼ãä¼éã«ã¯ä½¿ãã¾ãããã·ã°ãã«ãçºè¡ã§ããã®ã¯æ»ã¬ã¨ãã«éãããã§ããã¡ãã£ã¨ããºã¤ãã¨ããã£ãããã¨ãã£ã¦ãéºæ¸ãæ¸ãã¦æ»ãã§ãã¦ã¯ãï¼æåéãï¼ï¼å½ãããã¤ãã£ã¦ã足ãã¾ããã
çµå±ãæ£å¸¸å¤ãç°å¸¸å¤ãã¡ãã»ã¼ã¸ã§ä¼ãããããªãã®ã§ãok/erroræ¹å¼ã«æ»ã£ã¦ãã¾ãã¾ããok/erroræ¹å¼ãè延ï¼ã¯ã³ãï¼ãããã²ã¨ã¤ã®çç±ã¯ãErlangé¢æ°ã«ãã¡ãã»ã¼ã¸ã³ã°ãã©ããããã ãã®ãã®ãå¤ããã¨ãããã§ãããã
ä¾é¡ã«æ¬¡ã®é¢æ°ãèãã¦ã¿ã¾ãããã
find_data(Key) ->
gen_server:call(the_server, {find_data, Key}, 1000).
the_serverãåå¨ããªãã£ãããã¿ã¤ã ã¢ã¦ããçºçããã¨ãexitä¾å¤ãçºçããã®ã§ãåºæ¬çã«ã¯ãfind_data/1ãå¼ã³åºããã¯ã©ã¤ã¢ã³ãããã»ã¹ãæ»ã«ã¾ããããã¯ãä¸æ¸¬ã®ç°å¸¸äºæ ã¨èãã¦ããã§ãããã
ã§ã¯ãKeyã«å¯¾å¿ãããã¼ã¿ãè¦ã¤ãããªãã£ãã¨ãã¯ã©ãã§ããããthe_serverå´ã«å®è£ ãããfind_dataããã¤ã±ã«ãªã£ã¦exitãªã©ãã¦ã¯ãã¡ã§ãããµã¼ãã¼ããã»ã¹ãæ»ãã§ãã¾ããã¯ã©ã¤ã¢ã³ãããã»ã¹ãå·»ãæ·»ãã§æ»ã¬ã§ãããï¼é©åã«äºé²ãã¦ãªãã¨ï¼ãã¡ãã£ã¨ãããã¨ã§è²¬ä»»æ¾æ£ãã¦ãåãã«è¿·æãããã¦èªæ» ããããã¼ãããææªã
è¦æ±ããããã¼ã¿ãè¦ã¤ãããªãã¨ãããã¼ã«ã«é¢æ°ï¼åä¸ããã»ã¹å ã§ã®ã¿å®çµãã¦åãé¢æ°ï¼ãªãä¾å¤ãæããã®ãã¢ãªã§ãããããã»ã¹éRPCã§ã¯ããã¯ã{error, Reason}ã¡ãã»ã¼ã¸ãè¿ããã¨ã«ãªãã¾ãã
ãã ãã{error, Reason}ãåãåã£ãå´ãä¾å¤ãçºçãããæ¹æ³ã¯ããã¾ãã
find_data(Key) ->
case gen_server:call(the_server, {find_data, Key}, 1000) of
{ok, Value} -> Value;
{error, Reason} -> throw(Reason)
end.
find_data/1ã使ãå´ã¯ãããããã¼ã«ã«é¢æ°ãªã®ãã¡ãã»ã¼ã¸ã³ã°ãã©ãããã¦ããã®ãåãããªãã¨ããããã®ã§ãä»ã®é¢æ°ç¾¤ã¨ã®ãã©ã³ã¹ãèæ ®ãã¦ãéåæã®ãªã使ãåæãæä¾ããã®ãæã¾ããã§ãããã
âæ½ãã¨ç¡è²¬ä»»ã¯éã -- æ¦å£«éããã°ã©ãã³ã°
ãã¸ã¿ãã¿ããªãã§æ½ãæ»ããã¨ããããªã·ã¼ããã¾ãä¼ããã®ã¯ãªããªã大å¤ã§ããè¨ããããã¨ã¯ãåã ã®é¢æ°ãä¸æ¸¬ã®äºæ ã«å¯¾å¦ãã¦ï¼ãã®è¡¨ç¾ã¯èªç¾©çã«çç¾ãã¦ãããï¼ã´ãã£ã´ãã£ãã£ã¦ããããããªãããããããçµç¹ï¼ä½å¶ã®æ§é ã«ããé å¥æ§ãæ ä¿ãããã£ã¦ãã¨ã§ãã
å°ã£ã¦ãã¾ãã®ã¯ãè¥å¹²ã®ä¸é½åãæ³å®å ã®ã¨ã©ã¼ã®å¯¾å¦ã¾ã§ããæ¸ããªãã¦ãããã ãã¨ãããã«ãã誤解ã§ãããããªããããªãï¼ ããã¯ãåã«ãããã¨ãã£ã¦ãªããããã¡ãªã ãã§ããã¢ã¼ã ã¹ããã³ã°ã注æãã¦ã¾ãããã¦ã¼ã¶ã¼ã¤ã³ã¿ã¼ãã§ã¼ã¹ãçµç±ãã¦ããã¤ãã³ãï¼äººéãèµ·æºï¼ããWebã®POSTãã¼ã¿ãªã©ããæ¤æ»ãªãã§åãå ¥ããèªåã®é½åã«åããªãã¨æ»ãã§ãã¾ããããªããã°ã©ã ã¯ããæ½ãæ»ã¬ãããã°ã©ã ã§ã¯ããã¾ãããåã«ã¢ãã¿ã¬ã®ãã¡ããã°ã©ã ã§ãã
ãæ½ãæ»ã¬ãã¯ãèªåã®ä½¿å½ã¯ã¡ããã¨æãããã¨ããããä¸æ¸¬ã®äºæ ã«ã¯å¯¾å¦ã§ããªãã£ãããããããä¸æ¸¬ï¼äºæ¸¬ä¸å¯è½ï¼ãªãã ããæºåããã¦ãªãã£ãã¨ãããã¨ã§ããä¾ãã°ããã©ãã¯ã®é転æãèµ°è¡ä¸ã«å¤§å°éã«ãã£ã¦è·ç©ãç®çå°ã«å±ããããªãã£ãï¼æ»ãã§ãã¾ã£ãï¼ã®ã¯èª°ã責ããªãã§ããããããé¢åã ããè·ç©ãç©ã¿æ®ããã¨ããåæ¹ä¸æ³¨æã§æ°å®¶ã«çªã£ããã ãã¨ãã ã¨è¨±ããã¾ããã
çµå±ããã£ã¦ããããããªã対å¦ãæ¸ããªãããã«ã¯ãããã¹ããã¨ã¯ä½ãªã®ããå¼·ãæèããå¿ è¦ãããã¾ãããããã¹ããã¨ãã¯ãå½¹å²ã責åã使å½ã¨ãå¼ãã§ãããã§ããããããã¦ãã©ãã¾ã§ãäºæ¸¬ã§ããç¯å²ãã確å®ãããã¨ã§ããæ³å®å ã®ï¼ä»æ§ã§æ±ºããããï¼ç°å¢ã§ã¯è²¬åãæãããæ³å®å¤ã®äºæ ã«ã¯ä¸åè¨åããªãï¼ã³ã¼ãã«æ¸ããªãï¼ãããããæ½ãæ»ã¬ããã¨ã«ãªãã¾ãã
ããããæ¹éã¯ãErlang以å¤ã®ããã°ã©ãã³ã°è¨èªã§ãéããã¨ã¯æãã¾ãããErlangã§ã¯ãè¨èªä»æ§ï¼ã©ã³ã¿ã¤ã ã·ã¹ãã ããæ½ãæ»ã¬âæ¦å£«éãçããã°ã©ãã³ã°ãæ¨å¥¨ï¼ãµãã¼ããã¦ãã¾ãã