Can you name languages with static type checking (like Java) and where code is data (like in LISP)? I mean both things in one language.
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.)
F# has Quotation expressions. From the MSDN page:
// 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:
- Haskell has Template Haskell.
- Boo has 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'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.
Maybe Strongtalk or Zero which are reflective system a la Smalltalk, but are statically typed.
来源:https://stackoverflow.com/questions/3070369/do-you-know-of-a-language-with-static-type-checking-where-code-is-data