views:

241

answers:

7

Can you name language with Static Type checking(like Java) and where Code is Data(like in LISP)? I mean both things in one language.

A: 

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.

JAB
+4  A: 

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.

Nathan Sanders
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
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
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
+9  A: 

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.

Jörg W Mittag
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
+2  A: 

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.

Norman Ramsey
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
@Jacques good point, but they're still similar enough that I'm willing to put them in the same answer :-)
Norman Ramsey
+5  A: 

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

Eli Barzilay
A: 

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

ewernli
+1  A: 

http://en.wikipedia.org/wiki/Nemerle

dotneter