我正在阅读 Paul Graham 的“百年语言”一文。
http://www.paulgraham.com/hundred.html
在那里,他声称静态类型“排除了真正的宏”。
例如,类型似乎是研究论文的取之不尽的来源,尽管静态类型似乎排除了真正的宏——在我看来,没有它,任何语言都不值得使用。
这是真的吗?论文在哪里?我尝试在 Google 上搜索但没有成功。
我正在阅读 Paul Graham 的“百年语言”一文。
http://www.paulgraham.com/hundred.html
在那里,他声称静态类型“排除了真正的宏”。
例如,类型似乎是研究论文的取之不尽的来源,尽管静态类型似乎排除了真正的宏——在我看来,没有它,任何语言都不值得使用。
这是真的吗?论文在哪里?我尝试在 Google 上搜索但没有成功。
静态类型排除宏并不是真的,但这通常意味着宏系统需要比许多人熟悉的不卫生的文本替换宏更复杂。我不完全确定格雷厄姆在这里试图得到什么,或者他所说的“真正的”宏是什么意思。
有关静态类型上下文中的卫生宏的介绍,请参阅本文。
(顺便说一句,我也不同意没有宏的语言不值得使用。我认为一流的函数加上一些更复杂的现代类型系统可以优雅地解决宏所面临的大多数问题传统上是针对的。)
“静态类型似乎排除了真正的宏”这是怎么回事?
如果没有“真正的宏”的定义,这是一个毫无意义的主张,但在 Paul Graham 写之前,OCaml 既有静态类型又有(AST 重写)宏。他本可以很容易地为自己发现这一点,但显然没有。我什至没有理由相信静态类型会排除宏。
论文在哪里?
我不知道。Lisp 风格的宏现在已经过时了。
请记住,Paul Graham 是一名哲学专业的学生,他用 Lisp 编写了一个购物车,被雅虎收购并随后被重写。他挖到了金子,年轻时退休,然后花了几年时间写这样的东西。
Paul Graham is a proponent of having "the whole language there all the time":
There is no real distinction between read-time, compile-time, and runtime. You can compile or run code while reading, read or run code while compiling, and read or compile code at runtime.
Running code at read-time lets users reprogram Lisp's syntax; running code at compile-time is the basis of macros; compiling at runtime is the basis of Lisp's use as an extension language in programs like Emacs; and reading at runtime enables programs to communicate using s-expressions, an idea recently reinvented as XML.
So he may be referring to the practical impossibility of defining new types at runtime in a language that expects to have all the type information at compile time, as Daniel Ribeiro points out on his blog:
Note that you cannot make type-safe runtime metaprogramming in general. For instance: even though some languages allow you to create interfaces that do not exist on compile time, the only way to invoke methods from these is through non type-safe ways (such as reflection).
I am sure that you would have no trouble finding programmers who dispute that this capability is worth giving up the benefits that they derive from static typing.
系统不推断类型的静态类型阻止了真正通用的宏,因为宏的输出必须在非常严格的意义上保持类型安全。