Looking at most (if not all) dynamic languages [i.e Python, PHP, Perl and Ruby], they are all interpreted. Correct me if I'm wrong. Is there any example of dynamic language that goes through compilation phase? Is dynamic language identical with interpreted language?
Looking at most (if not all) dynamic languages [i.e Python, PHP, Perl and Ruby], they are all interpreted.
Not true. You can compile Python source. That's one existential proof.
There are interpreters for statically-typed languages, and compilers for dynamically-typed languages. The two concepts are orthogonal.
Side note: In general, a language is just that: a language, with a set of syntactic constructs to express semantics. If you write Python on a whiteboard, it's still called Python! It's the implementation that can be an interpreter or a compiler. Being statically-typed or dynamically-typed (of kind of a hybrid of both) is a property of the language, while executing a program by interpreting or compilation is a property of the implementation.
Common Lisp is dynamically (and strongly) typed and usually compiled.
Since this dynamic-ness is achieved at runtime, there are some directives you can use in source code to assure the compiler that a symbol will hold only a certain kind of value, so that the compiler can optimize the generated code and boost performance.
C# 4.0 supports dynamic types (late-binding) and it is compiled.
Once upon a time, BASIC was interpreted. And some variants of BASIC had dynamic typing. And you could get compilers for them as well.
(This was back in the days of 100K floppy drives, when dinosaurs still roamed the earth and ate unsuspecting s/w developers for breakfast.)
Different Smalltalk implementations handle this differently, but several of them compile to bytecodes that run on a high-performance VM.
In fact most of the so called "interpreted" languages go through / allow a just-in-time compilation to make it run faster. And some of them has to be compiled to byte code before you can run them.
In fact dynamic and interpreted are totally 2 different ideas, though there is an correlation. The reason being who ever feels the dynamic typing makes their job easier and faster, they wouldn't mind the code to be run a bit slower but portable.
No - it's certainly possible to compile dynamic languages.
There are even some dynamic languages that are always compiled by design (e.g. Clojure).
The question however touches on an important related point: although dynamic languages can be compiled, it is often the case that dynamic langauges cannot be compiled down to code that is as efficient as a statically typed language. This is because there are some inherent features in dynamic languages that require runtime checks that would be unnecessary in a statically compiled langauge.
An example of this: languages that allow runtime patching of objects (e.g. Ruby) often require the object to be inspected (with a hashtable lookup or similar) whenever you invoke a method on the object. Even if this is compiled, the compiler will have to generate code to do the method lookup at runtime. To some extent this method lookup is not dissimilar to what an interpreter would have to do.
This adds a significant overhead when compared to a method call in a language like Java, where the correct method can be statically determined by the compiler from the class definition and reduced to a simple function call in native code.
I believe it is this effect more than anything else that results in dynamic languages having slower performance on average than their statically compiled counterparts. As you can see from the flawed benchmarks, it's the statically typed languages (C, Java, Fortran etc.) that tend to be fastest with the dynamic languages (Perl, Python, Ruby, PHP etc.) at the bottom of the ranking.
I think the reason that dynamic languages historically tend to be interpreted is because dynamic typing and interpreting (or more specifically, the lack of compiling) both tend to be features useful to scripting languages and scripting tasks in general.
Performance also isn't (wasn't) as much of a concern for the kinds of programs that were written in these languages, so again, the overhead of dynamic typing and interpreting wasn't as big an issue as it would be in languages that value performance.
Python is, typically, compiled. Admittedly compiled to byte code that is then interpreted.
Perl works in a similar fashion.
Common Lisp will, typically, compile to one of native or byte code. This differs between implementations (and, to some degree, within an implementation, depending on various optimization settings).
Yes. All dynamic languages are interpreted language (but an interpreted language could be non-dynamic).
The reason is simple : if it is dynamic, it needs an interpreter to perform the dynamism at the level of the binary compilation.
ex. : when we put a data in a PHP variable, then later another one of a different type, our program could not compile into binary code since each type has its own binary representation format ; the interpreter manages the shifts at the binary level in a dynamic way
protected by gnat Aug 29 '17 at 13:11
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?