Dynamic languages have been gaining support and interest over the last few years. Static languages however still dominate usage, and have benefits, like performance and verifiability, that some dynamic languages would like to replicate. At the same time, some static languages are realizing the value of dynamic languages and are attempting to replicate some of those benefits, while retaining the static capabilities. I have attempted to create a list of the dynamic features that static languages are picking up on, and how they are addressing them. I am sure this list is not yet complete as most there are a great deal of interesting languages out there these days, and I have not had a chance to review them all. If you know of a few more features or implementations, let me know. If your interested in static/dynamic crossover, you might be interested in three earlier posts. Inform, Suggest Safety and Allow Freedom, Static/Dynamic? Why not both? and Principles of Language Design – Semantic Capture Safe features do not require late binding and retain all static type checking ability, while shortening syntactical definition. Sometimes this results in a clearer syntax as well. Safe features stretch from syntactic translations, like type inference, to functional extensions like lambda expressions. Found in: C# 3.0, Visual Basic 9, F#, Fortress, ML, OCaml, Haskell, Boo, Nemerle, Groovy?, CleanSafe Features
Type Inference
Type inference is the simulation of dynamic syntax while maintaining static typing. A variables type is not explicit, the compiler infers it from the context. The depth of inference can vary, but usually takes into account at least constructors for local variables.
Note: I could not really tell whether the def statement in Groovy was a dynamic variable or a type inference mechanism.
Structural Subtyping
Found in: Haskell, ML, OCamlAlso know as: ad hoc interfaces
Structural subtyping can be thought of type inference, where the name of the type is not taken into account, but only the structure of the type. The ultimate result is very similar to duck typing, while remaining completely statically typed.
Anonymous types
Found in: C# 3.0, Visual Basic 9, haXe, OCaml
Also know as: immediate objects
Anonymous types allow using dynamic syntax for temporary types or data structures. Under the covers, these are just another type with an implicit name.
Tuples
Tuples are very similar to anonymous types, but tuple members do not have names. Technically I believe the term tuple could be used to refer to anonymous types as well, but in practice where the term tuple was used it was with unnamed members.
Lambda Expressions
Found in: C# 3.0, Nemerle, ML, OCaml, Groovy, Boo, Nice
Generics
Found in: C# 2.0 Visual Basic.NET, Java 5, Managed C++, Nemerle, ML, OCaml, etc.
It is somewhat debatable if generics are a dynamic feature. They fulfill a very specific piece of the self-generating program capability available from many dynamic languages, but do it in a very different way.
Unsafe Features
Unsafe features shift evaluation to runtime through techniques such as reflection, lazy evaluation, or runtime interpretation.
Runtime Casts
Found in: C#, Visual Basic, Java, C++, Nemerle, Boo, etc.
Not commonly considered a dynamic feature, casts that evaluate type coercion to at runtime are a dynamic feature. In statically typed languages, runtime casts usually only result from explicit casting, yet the two terms are not synonymous.
Dynamic variables (a.k.a. Duck Typing)
Found in: Visual Basic.NET, haXe, Nemerle, Boo
A dynamic variable is quite simply a variable that not only can hold an object of any type, but allows any member name or signature to be valid at compile time. Some languages like Visual Basic enable/disable this globally or per file. Other languages have a specific dynamic type.
Dynamic interfaces
Found in: Visual Basic 9, Nice
A dynamic interface allows for after the fact interface definition. A dynamic interface is similar to a dynamic variable, but defines some restrictions. Like a dynamic variable, a dynamic interface at compile time accepts assignment from variables of any type.
Dynamic blocks
Found in: haXe
A dynamic block allows evaluation of a series of statements to either partially or fully at runtime.
Known implementation techniques
Generate reflection code. Miniature interpreter.
Dynamic identifiers
Found in: Visual Basic 9
A dynamic identifier allows evaluation of individual identifiers at runtime. In many ways, dynamic identifiers are reflection short hand, but their syntax is similar to that of dynamic languages.
3 comments:
I'm presuming that by "static"/"dynamic" languages you mean statically or dynamically typed. In which case, calling type inference a feature of dynamic languages makes no sense, AFAICT. Type inference is only useful if you're checking types before runtime.
And ML is pretty much the canonical example for a language that provides type inference, so listing O'Caml there isn't too interesting in any case.
OCaml actually supports all of the safe features you list, as well as dynamic interfaces (which they call 'structural subtyping'). Dynamic interfaces can be inferred at compile time, and this is how type inference works with their object system, I believe. I am not a Caml expert, but check the second paragraph of the Wikipedia section on Features: http://en.wikipedia.org/wiki/OCaml#Features
I think that a better title would be 'features that simulate dynamic typing by using static typing', since the features you list (except for lambda) are all things *not* associated with dynamically typed languages, but necessary for statically typed languages to provide similar flexibility.
I read your other posts on this subject and thought your 'unsafe dispatch' block idea was interesting. Do you know about Lambda the Ultimate (www.lambda-the-ultimate.org) ? It's a blog and forum for language enthusiasts that focuses on language design issues. It tends toward the academic, but from time to time really interesting things show up, eg I saw the Static Typing Where Possible/Dynamic Where Needed paper there shortly after it was posted.
No, I would definitely describe type inference as a dynamic language feature. It's not common to all dynamic languages, but many, like Python include or support it. The inference doesn't get converted to a static type but it exists nonetheless.
Some of these do fit into simulating dynamic typing, but many are simply accomplishing the same task (feature) using static typing.
For example, dynamic blocks is a good example of a simulation, but type inference is not simulation at all.
I'll work on the ML/OCaml parts. I am in the middle of learning the languages at the moment and I've found it much more difficult to drag the features out of the docs then the other examples I've listed.
Thanks for the links, and yes Lambda is great.
Post a Comment