Qi is a statically-typed Lisp dialect. Also, many other Lisp dialects have (optional) static typing.
Java itself has very limited capabilities of this kind.
The interesting question is not so much whether you can have metaprogramming and static typing, it's whether you can have dynamic metaprogramming be statically type-safe.
There is Template Haskell which does metaprogramming and is type-safe, but it is static metaprogramming.
At the moment I can not think of any language that I know for a fact allows dynamic metaprogramming and where dynamic metaprogramming is statically type-safe. Qi might be bale to do it, but I'm not sure.
Racket (formerly PLT Scheme) has a statically typed dialect, which is designed to work nicely with Scheme idioms -- including macros. (It works by type-checking the expansion results.)
// typed
let e : Expr<int> = <@ 1 + 1 @>
// untyped
let e' : Expr = <@@ 1 + 1 @@>
// splicing with %
// similar to Lisp's unquote-splicing, but type-checked:
// you can only splice expressions of the appropriate type
<@ 1 + %e @>
// typed
let e : Expr<int> = <@ 1 + 1 @>
// untyped
let e' : Expr = <@@ 1 + 1 @@>
// splicing with %
// similar to Lisp's unquote-splicing, but type-checked:
// you can only splice expressions of the appropriate type
<@ 1 + %e @>
I think these are available in C#, but (1) I don't know what the syntax is (2) the data structures are different.
These languages allow code as data at compile time, like Lisp macros:
O'Caml has camlp5, which is a sophisticated preprocessor.
Disclaimer: I haven't really used any of these. As far as I know, they are all much more complicated than Lisp's quote.
However, 90% of "Code as data" using quote can be accomplished with closures, since they delay evaluation too. Many languages have a convenient syntax for making closures (C#, Clojure, Scala and Ruby especially come to mind) and don't need quote that much. Even in Scheme, which is a definitive Lisp, the prevailing style favours passing functions over writing macros.
Template Haskell is statically typed but allows you to manipulate code as data, aka metaprogramming. Related languages include MetaML and MetaOCaml. Look up the work of Tim Sheard.
If you need more than that (want methods and classes as first-class objects, etc.), then you'll want to use something like Haskell or C# (as mentioned in other answers) instead.
发布评论
评论(6)
Qi 是一种静态类型的 Lisp 方言。此外,许多其他 Lisp 方言都有(可选)静态类型。
Java 本身的此类功能非常有限。
有趣的问题不在于是否可以拥有元编程和静态类型,而在于是否可以使动态元编程静态地类型安全。
Template Haskell 可以进行元编程并且是类型安全的,但它是静态元编程。
目前,我想不出任何我所知道的语言确实允许动态元编程并且,其中动态元编程是静态类型安全的。齐也许可以做到这一点,但我不确定。
Qi is a statically-typed Lisp dialect. Also, many other Lisp dialects have (optional) static typing.
Java itself has very limited capabilities of this kind.
The interesting question is not so much whether you can have metaprogramming and static typing, it's whether you can have dynamic metaprogramming be statically type-safe.
There is Template Haskell which does metaprogramming and is type-safe, but it is static metaprogramming.
At the moment I can not think of any language that I know for a fact allows dynamic metaprogramming and where dynamic metaprogramming is statically type-safe. Qi might be bale to do it, but I'm not sure.
Racket(以前称为 PLT 方案)有一个 静态类型方言,其设计目的是与Scheme 习语(包括宏)很好地配合使用。 (它的工作原理是对扩展结果进行类型检查。)
Racket (formerly PLT Scheme) has a statically typed dialect, which is designed to work nicely with Scheme idioms -- including macros. (It works by type-checking the expansion results.)
F# 有引用表达式。来自 MSDN 页面:
我认为这些在 C# 中可用,但是(1)我不知道语法是什么(2)数据结构不同。
这些语言允许代码在编译时作为数据,就像 Lisp 宏:
免责声明:我还没有真正使用过其中任何一个。据我所知,它们都比 Lisp 的
quote
复杂得多。然而,90% 使用
quote
的“代码即数据”可以通过闭包完成,因为它们也会延迟评估。许多语言都有方便的语法来创建闭包(尤其是 C#、Clojure、Scala 和 Ruby),并且不需要那么多的quote
。即使在Scheme(这是一个权威的Lisp)中,流行的风格也更倾向于传递函数而不是编写宏。F# has Quotation expressions. From the MSDN page:
I think these are available in C#, but (1) I don't know what the syntax is (2) the data structures are different.
These languages allow code as data at compile time, like Lisp macros:
Disclaimer: I haven't really used any of these. As far as I know, they are all much more complicated than Lisp's
quote
.However, 90% of "Code as data" using
quote
can be accomplished with closures, since they delay evaluation too. Many languages have a convenient syntax for making closures (C#, Clojure, Scala and Ruby especially come to mind) and don't needquote
that much. Even in Scheme, which is a definitive Lisp, the prevailing style favours passing functions over writing macros.Template Haskell 是静态类型的,但允许您将代码作为数据进行操作,又名元编程< /em>.相关语言包括 MetaML 和 MetaOCaml。查阅蒂姆·谢尔德的作品。
Template Haskell is statically typed but allows you to manipulate code as data, aka metaprogramming. Related languages include MetaML and MetaOCaml. Look up the work of Tim Sheard.
如果您只是寻求以静态类型语言动态执行代码的能力,那么 Java 本身就可以做到这一点:
http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html
如果您需要更多(想要方法和类作为第一类对象等),那么您将需要使用 Haskell 或 C#(如其他答案中提到的)之类的东西。
If you're simply looking for the ability to execute code dynamically in a statically typed language, then Java itself can do that:
http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html
If you need more than that (want methods and classes as first-class objects, etc.), then you'll want to use something like Haskell or C# (as mentioned in other answers) instead.
也许是Strongtalk 或Zero 是反射系统 a la Smalltalk,但是是静态类型的。
Maybe Strongtalk or Zero which are reflective system a la Smalltalk, but are statically typed.