4 ms·
The main thing that I see dynamic languages as having over static languages is metaprogramming. See http://stackoverflow.com/questions/8276387/statically-typed-
by lihaoyi 14y ago
The main thing that I see dynamic languages as having over static languages is metaprogramming. See http://stackoverflow.com/questions/8276387/statically-typed-metaprogramming http://stackoverflow.com/questions/8276387/statically-typed-....
Metaprogramming is prevalent in python/ruby, regardless of how confusing it is, because it is super useful. Being able to dynamically construct types and classes (e.g. via decorators in python) allows you to do tons of stuff that is otherwise really hard for library writers, even though you shouldn't be using metaprogramming all over the place day-to-day. Rails, SQLAlchemy and a ton of other libraries/frameworks use metaprogramming all over the place.
Static languages lack this ability, but because it's so useful, they end up re-implementing the same thing anyway but using reflection/bytecode-manipulation/extra-compile-hooks/code-generation/etc.. like in Spring/AspectJ/Play/ASP.NET!
Although the end result is kinda the same, I'd argue metaprogramming via generating java source code is way harder than metaprogramming in python, where creating a new class and setting its methods or descriptors is as easy as creating/fleshing out any other object. Given how powerful/useful metaprogramming is, I think that that's the biggest gain of dynamic over static languages.
- darkstalker 14y agoThat's possible because the language runs on a VM that contains metadata about your data structures, not because of the dynamic typing. If classes are implemented as a hash table of it's fields, then it's easy to add/remove members at runtime.
- colanderman 14y agoStatic languages lack this ability cough MetaOCaml [1] cough cough first-class modules in OCaml [2] cough explanation -- MetaOCaml provides what is essentially a typed eval for Objective Caml. OCaml's first-class modules provide the ability to construct types, classes, and modules at run time. In other words, statically typed languages do have this ability. [1] http://www.metaocaml.org/ http://www.metaocaml.org/ [2] http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual021.html#toc81 http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual021.ht...
- icebraining 14y agoCould you please stop with the "cough" parts? It's rude and it adds nothing to the discussion.
- strictfp 14y agoI find this type of introspection highly annoying in any language. If the lib/framework is poking around in your class internal data you loose control over your objects. The useful abstraction which an object provides is gone and it's reduced to a struct. As an example it is hard to dynamically generate the contents in Djangos Meta-class. Another example is that it's very hard to keep immutability and maintain invariants in Hibernate entities. I'd like to say the following to the creators of similar frameworks: Just give me the interface and I will implement it. And keep your hands off my internals!
- bunderbunder 14y agoWhat makes Python's metaprogramming fundamentally different from the dynamic class generation that exists in statically typed languages such as C#? And what about it is facilitated by dynamic typing? Part of why I suggest C# is that a lot of .NET libraries make heavy use of this functionality. Windows Communication Foundation, NHibernate, and Moq come to mind. So my impression had always been that it was approximately the same level of reflection, just with perhaps more syntactic sugar in one case.