(1) Reflection
程式執行期間,利用外部來的字串來觸發函數。使用這個的話,可以減少一些重複的 switch 邏輯。
Clojure -> ns-resolve
Python -> getattr
範例:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
#!-*- coding:utf8 -*- | |
import sys | |
def hi(): | |
print("hello world") | |
getattr(sys.modules[__name__], "hi")() |
(2) Eval
程式執行期間,增加程式本身 (program) 的功能。因為可以透過 Eval 定義新的函數。
// Clojure, Python 辦得到。但是 Go 不行。
(3) Lisp Macro
程式執行期間,增加 interpreter 的功能。因為 macro 可以視為是一種 compiler plugin 。它可以增加程式語言本身的新的語法。此處所謂的新的語法,包含了 syntax 和 semantic 。 semantic 的部分,自然是改變了 interpreter 的行為才能辦到。
// Clojure, Hylang, Smalltalk 辦得到。