need_result
// (2007/6/12) ééãã¦ããã®æ¥ã®æ¥è¨ãä¸åæ¶ãã¦ãã¾ãã¾ããã
æè¿å®é¨çã«è¿½å ããæ©è½ã§ãneed_resultãã¨ãããã®ãããã¾ãã
æå³ã§ãããããã¯ãé¢æ°å¼ã³åºãå ããæ»ãå¤ãå¿ è¦ã¨ãã¦ãããã©ããããtrueãfalseãã§è¿ãæ¬ä¼¼å¤æ°ã§ãã
ä¾ã§ç¤ºãã¾ãã
foo: fun(){
if(need_result){
"yes".p
}else{
"no".p;
}
}
a: foo(); //=> yes
foo(); //=> no
ãã®ã¨ãããé¢æ°å¼ã³åºãã®çµæã使ãããªããããªé¢æ°å¼ã³åºãã§ãneed_resultã¯falseã¨ãªãã¾ãã
å©ç¨æ¹æ³ã¨ãã¦ãä¾ãã°ãå¯ä½ç¨ãç¡ãé¢æ°ãªã®ã§ãæ»ãå¤ã使ãããªããããªä½¿ãæ¹ãããããã¨ã©ã¼ã«ããããå ´åã¨ã
fib: fun(i){
assert need_result;
if(i<2){
return 1;
}else{
return fib(i-2) + fib(i-1);
}
}
fib(30).p; // çããåºåããã
fib(30); // assertion error
ãæ»ãå¤ãè¦ããªããªããè¿ãå¤ã®è¨ç®ã¯ããªããå ´åã¨ã
fib: fun(i){
if(!need_result){
return;
}
if(i<2){
return 1;
}else{
return fib(i-2) + fib(i-1);
}
}
fib(30).p; // ããããæéãããã
fib(30); // ä¸ç¬ã§çµãã
ãã®ããã«ãé¢æ°å¼ã³åºãå´ãæ»ãå¤ã欲ããã£ã¦ãããã©ããã§å¦çãåå²ããããã¨ãåºæ¥ã¾ãã
ãã¦ãä»åãã®ãneed_resultãã«æ¿ãããæ°ããªå称ãåéãã¾ãã
ç¾å¨ãã®ãneed_resultãã¯äºç´èªãªè¨³ã§ããã©ããå¾®å¦ãªæãã§ããã¢ã³ãã¼ã¹ã³ã¢ãå ¥ã£ãäºç´èªã¨ãè¦ããã¨ããã¾ããã
ãã¨ã以ä¸ã®ãããªæãç°¡åã«æ¸ããç¹å¥ãªreturnãæä¾ãããã®ã§ãããããã®å称ãåéãã¦ã¾ãã
foo: fun(){
return_if_need_result 5 + 5;
/* âãâã¨åãæå³ã¨ãªãç¹å¥ãªreturn
if(need_result){
return 5 + 5;
}else{
return;
}
*/
}
ãããããããªæ©è½ã¯è¦ããªãããããªããçã®æè¦ãåéãã¦ããã¾ãã