Take the 2-minute tour ×

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.

share|improve this question
I would tend to agree with the answers to this question: stackoverflow.com/questions/2953141/… that it is simply not possible to have both. –  Daniel Pryden Jun 18 '10 at 14:33
@Daniel: Only the lowest-voted answer to that question claims that it's not possible to have both. It does this by claiming that the typing would be really hard. Hard, yes, but not impossible. –  Nathan Sanders Jun 18 '10 at 15:19
This is very similar to stackoverflow.com/questions/3037643/… –  Jacques Carette Jun 18 '10 at 20:21

7 Answers 7

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.

share|improve this answer
Can I ask for a clarification of terms? I'm not sure I understand your point as is. What do you mean by dynamic versus static metaprogramming? Is static metaprogramming "at compile time", ie macros, vs dynamic, "at run time" ie arbitrary expression quotation? –  Nathan Sanders Jun 18 '10 at 15:21

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.)

share|improve this answer

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:

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.

share|improve this answer
Macros are not 'compile time'. Macros work perfectly well in Lisp interpreters. Lisp Macros also are not 90% about delaying evaluation. Macros have a lot of purposes. Most have nothing to do with delaying evaluation. –  Rainer Joswig Jun 18 '10 at 16:25
Macro evaluation occurs at 'read time' in Common Lisp and Scheme (as far as I recall). I did not know whether the asker would recognise the term so I used a less accurate but more common one. There have been experimental Lisps with first-class macros, but they're not widespread. As for delaying evaluation, that's precisely what quote is for, and what closures give you as well, albeit less conveniently. I was not talking only about macros, since they are a restricted use of code-as-data. –  Nathan Sanders Jun 18 '10 at 17:03
Nathan, macro evaluation sure does not happen at 'read time' in Common Lisp. Not a bit. Macro expansion happens during interpretation and/or compilation and that is described in the CL standard. If code runs interpreted, then macros can be expanded during interpretation. QUOTE is also not for delaying evaluation like 'closures'. QUOTE is there to denote literal data. Quoted code is not delayed, so that it can be 'forced'. If one wants to executed quoted code, then it has to be evaulated. That's a huge difference. –  Rainer Joswig Jun 18 '10 at 17:14

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.

share|improve this answer
MetaML and MetaOCaml are quite different than TH - the former are doubly-typed (typed generators guarantee to only generate typed code) while TH is only singly-typed, i.e. only the results of template expansion is type-checked. –  Jacques Carette Jun 18 '10 at 20:19
@Jacques good point, but they're still similar enough that I'm willing to put them in the same answer :-) –  Norman Ramsey Jun 19 '10 at 0:31

If you're simply looking for the ability to execute code dynamically in a statically typed language, then Java itself can do that:


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.

share|improve this answer

Maybe Strongtalk or Zero which are reflective system a la Smalltalk, but are statically typed.

share|improve this answer

Your Answer


