fletã§é¢æ°ã横åããã
ãã®è¨äºã¯Emacs Advent Calenderã®4æ¥ç®ã§ããã¯ããã¦ã®advent calenderã§ä½ãæ¸ãã°è¯ããèãã¦ãã®ã§ãããemacs(emacs lisp)ãªãã§ã¯*1æ©è½ãç´¹ä»ãããã¨ã«ãã¾ãã
dynamic scope / lexical scope
æè¿ä½¿ãããè¨èª*2ã®å¤ããlexical scopeãæ¡ç¨ãã¦ããã®ã«å¯¾ããemacs-lispã¯dynamic scopeã§ããlexical-scopeã¯å¤ã®æç¸ãå®ç¾©ã®éã«æ±ºã¾ãã®ã«å¯¾ããdynamic-scopeã¯å®è¡æã«æç¸å¤ãå®ã¾ãã¾ãã
ã¨ãããããããã§lecial-scopeã®schemeã¨æ¯è¼ãã¦dynamic scopeã¨lexical scopeã®éãã®ä¾ãç´¹ä»ãã¾ãã
dynamic-scopeã®å©ç¨æ³
dynamic-scopeã ã¨ä½ãããããã®ã§ããããï¼ç°¡åãªä¾ãç´¹ä»ãã¾ãã
å©ç¨ä¾1:è¨å®ãããæ²ãã
ä¾ãã°ãç¾å¨è¨å®ããã¦ããç¶æ ãããæ²ãããã¨ãã§ãã¾ããbuffer-read-onlyãtã®æãé常bufferã¯read-onlyã«ãªãããããã¡ã«æåããããªããªãã¾ããå½ç¶insertã使ãã¾ããã
(setq buffer-read-only t) ;; C-x C-eããã¨æ¸ããªããªãã¾ãã (let ((buffer-read-only nil)) (insert "foo")) ;; å®è¡ããã¨fooãæ¿å ¥ããã¾ãã
ä¸æ¹ãããã§ãletã§buffer-read-onlyãnilãæç¸ããç°å¢ã§ã¯insertã使ãã¾ãããããã以åã¨ãã¦ããã®ãããã¡ã¯read-onlyã®ã¾ã¾ã§ãã
å©ç¨ä¾2:å®ç¾©ãããé¢æ°ãæ¡ãã¤ã¶ã
ããã«ãä¸æçã«ã·ã³ãã«ã«æç¸ãããé¢æ°ã®ä¸èº«ã横åããã¦ããããããã¨ãã§ãã¾ããfletã使ãã¨ä¾¿å©ã§ãã以ä¸ã®ã³ã¼ãã®timerãæå¹ã«ããã¨ã1ç§ããã«ããããããã¡ã«ã¡ãã»ã¼ã¸ã表示ããã¾ããããã¦ãããã¯ãM-xã§ã³ãã³ããå®è¡ããæãªã©ãããããã¡ã§æåãå ¥åãã¦ããã¨ãã«ãç¶ãã¾ãã
(setq timer (run-with-timer 1 1 (lambda () (message "I'm working. (annoying message)")))) ;; æå¹ã«ããã¨ãããããã¡ã«ã¡ãã»ã¼ã¸ã表示ãã¦ãã¦ãããã (require 'cl) ;;ãããããã¡ã§ã®å ¥åãä¸ã®timerã«éªéãããã (read-string "read:") (flet ((message () nil)) ;;ãã®ä¸ã§ã¯messageã®å®ç¾©ãç½®ããããã (read-string "read:"))
ãã®ã¡ãã»ã¼ã¸ãæ£ç´ã«è¨ã£ã¦ã¦ã¶ãã§ãããããããã¡ã«å ¥åãã¦ãããæãããéãã«ãã¦ã»ããã§ããããããfletã§messageãä¸æçã«ä½ãããªãé¢æ°ã«ç½®ãæãã¦ä¸ãããã¨ã§ãéªéããããã¨ãªããããããã¡ã§æåãå ¥åãããã¨ãã§ããããã«ãªãã¾ããdynamic-scope++
使ã£ã¦ããelispã®å«ãªç¹ã®å¤æ´ãªã©ã«fletã使ã£ã¦ã¿ãã®ãè¯ãããããã¾ããã