Do you know of a language with Static Type checkin

2020-06-01 01:49发布

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.

6条回答
虎瘦雄心在
2楼-- · 2020-06-01 02:23

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

查看更多
Juvenile、少年°
3楼-- · 2020-06-01 02:26

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

查看更多
走好不送
4楼-- · 2020-06-01 02:28

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.

查看更多
看我几分像从前
5楼-- · 2020-06-01 02:28

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.

查看更多
Explosion°爆炸
6楼-- · 2020-06-01 02:40

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.

查看更多
够拽才男人
7楼-- · 2020-06-01 02:41

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.

查看更多
登录 后发表回答