Do you know of a language with Static Type checking where Code is Data? [closed]

*爱你&永不变心* 提交于 2019-12-03 05:57:45

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:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!