Typed and untyped programming languages

Asked

Viewed 6,965 times

19

Why (I don’t know if it’s a rule) are interpreted programming languages (e.g., PHP) not typed and compiled (e.g., C) typed? (Java in this case is a hybrid).

Why do they have these differences?

3 answers

17


Typing

In essence all programming languages are typed, that is, all data is typed, and concretely the computer needs to have a type, even if it is unique.

Dynamic X static

Strictly speaking, there are languages with dynamic typing and static typing. Dynamic typing variables are said to have only one type and data have one type tag together. Then the variable can have a tag different depending on the data stored in it. Speaking more informally we can say that the variable has a different type according to the data saved.

The data (the object) itself always has its own type. What is said in dynamic typing has to do with the variable only.

Of course static typing gives more robustness and performance to the code since it doesn’t have to keep checking at runtime on types, so compilation fits well into this, after all the main points that compilation gives is robustness and performance. It is strange for a language running in an interpreted way to want to have a robust and performance typing, as well as a compilation that gives it.

By "typed" you mean that the variable has its static type. We can combine like this?

Most of what you want to know is already What is the difference between a static and dynamic programming language?.

Some languages with dynamic typing are adopting the explicit type of variables. It helps a little in robustness if the compiler can handle it well, so errors can be detected more easily. Of course, if the language is interpreted helps little, but it helps if you do a lot of testing. If it is compiled it already detects before.

This new form is often called hinttype, because you don’t make the variable have only one type throughout its existence, just say it’s the type to be considered, so it doesn’t improve performance, but it facilitates the scale of development.

Dynamic typing is flexible and helps to develop faster (very little, and if it is well tested, it takes longer, there are studies that show it), so this type should only be used on a small scale, to scripts, not for applications.

How languages like PHP, Python, Ruby and even Javascript started to be used in applications, or authors should tell you not to do this, or embrace this new audience and adopt the explicit type where you give and that’s what they started to do, even JS through Typescript.

But make no mistake, there is still dynamic typing, everything is solved at runtime.

In the background virtually all dynamic typing implementations make use of a union tagged in C, something like that:

struct data {
    int type;
    union data {
        int integer;
        double real;
        char * string;
        void * array;
        ...
    }
}

I put in the Github for future reference.

The Runtime of the language then never access the data directly, it checks what is inside it and decides what to do during the execution according to what is marked in type

Why do they have these differences?

For the comfort that each one gives.

As already said static typing gives more robustness and performance, and makes it easy to work with large applications.

Dynamic typing takes a bit of the ceremony of having to say the type and allows a variable to be reused for various things (which is questionable), which gives an initial gain in development time and if it is a code throw away, that gain is important. If the code survives longer then it starts playing against.

In some scenarios flexibility and simplicity are preferable to robustness and performance.

So it has different tools for each case. That’s why it has screwdriver and Phillips.

Wrong premises in question and answers

There is an error in the premise of the question in finding that PHP is interpreted or C is compiled. There is PHP compiled and C interpreted. And there is no relationship between typing and the way the code is executed.

Java has nothing hybrid, it is compiled. Just like C#. Of course it may have interpreted implementations, but the default is not so and in any language it can have both.

The typical languages of script usually compiled, contrary to popular belief. PHP is the only one of the best known that this is more limited, but it is still compiled in each run (in will change more in PHP 8). Interpretation is a more interactive and almost abandoned process except when the execution needs to be interactive even.

PHP has always typed all the data. And in PHP 7 it’s still like that, now he can give a little help when you explain the type, but he hasn’t changed the typing system.

I think PHP and JS are the only ones we can call hybrid because it compiles and runs every time (JS is long gone and PHP 8 is Jittado). At least that’s how we use it. Others are compiled for one bytecode, then there is no interpretation. There is no direct execution of a binary code, but this is different from interpretation.

Even Lua people forget how language like that is. And I’m not even talking about Luajit which is even more interpreted and then it can be considered hybrid. And Luajit beats performance of all these best known, in addition to being simpler. Arrives to compete with languages like C in some scenarios.

C has nothing hybrid in its normal form, not even using Clang. What exists is a clear separation between the build part in the frontend and in the backend of the compiler, as happens in virtually every modern compiler, including GCC, MSVC.

Some consider Java and C# to be hybrid because native code is only generated at run, but the build itself has already occurred, it only has one extra step, one backend, and that is far from interpretation, in any definition of that.

  • Thank you very much for the answer, very enlightening! I really had to reconsider my concept about Java.

  • Now that the real author has spoken +1 :)

12

Actually, it’s not a rule. You will certainly find exceptions on both sides. What happens most is a classification error about what is interpreted and what is compiled: this line was certainly blacker in white at the time that computadors had 64KB of memory in total.

Another important definition that is incorrect in the question - "not typed" is different from "dynamic typing" - shell script may be called "not typed" (everything is a string, and each command/program interprets these strings in a way, depending on the position, whether they correspond to an option tag, etc...) - but languages like Javascript, Python Ruby, PHP have yes types - only the type is determined at runtime - that is "dynamically typed" - those that have the type given in compile time and that cannot be changed, are "statically typed" (and it is not always necessary for the type to be explicit in the declaration of a variable - the compiler can infer the type from the content declared, for example).

But in addition to the Java you mentioned, Python is also "compiled for a virtual machine that has bytecode interpreted". That is, both are "hybrids", but because Java is statically typed and Python is dynamically typed, it is quite common for people to say that "Python is interpreted", while no one says this from Java. In other words: there is an almost inverse relationship - people assume that Python is interpreted only because it has dynamic typing. C itself works this way "hybrid" if you use the Clang compiler, which compiles for LLVM - a very optimized virtual machine similar to Python and Java, instead of native code.

But, yes - it’s based on this perception that you have - and the reason why more compiled languages have static typing is precisely that the _de_memory space_reserved to keep the variables is created at compile time. For example in C, if the compiler "sees" a variable of type "char", followed by a declared "int", it knows that it will need 1 byte for one and 4 (in general) bytes for the other - and already leaves a space of 5 bytes in the appropriate address area.

That is - in a program in C, everything that relates to the size of a variable, the machine instructions to write and read its values, etc... are solved at compile time - (including sizeof is not a function, but a special instruction for the compiler). The code of the program while running does not know anything about the type of variables, nor their names. (although the vast majority of programs are compiled with built-in debugging information, so these metadata are present, yes, in binary files).

Already, the basic requirement for a dynamic typing language is that the variable content itself has information about its size in memory, and how it can be used. In Python for example, all types derive from "Object", which in its layout in memory has in the first bytes a header - and one of the fields in this header is precisely the total size of the object in memory.

Other languages will use different strategies - but in general, internally, they will pass a memory address (which in C would be a pointer) each time a variable is passed as a parameter - and if, at any time during execution, an operation is made incompatible with the data type at that address, an execution error (Runtime error) occurs. If the language does not have protection for this type of access, it can be a Segmentation fault, and the process is terminated. Otherwise, an exception is thrown within the language rules themselves that can be captured - acntece with Python and Postscript, for example.

8

Typing itself is a feature of each language being compiled or not. A good example is PHP itself.

As of php 5 it has 2 types of typos (arrays and objects with class names). Already with php 7 it happens to have other types for typing that although optional is still a typing.

  • Could you tell me more about this "non-cooling" of earlier versions of PHP 7 and what exactly has changed in version 7? As far as I know, the only change you’ve made in this respect is allowing hinttype, but this has no relation to be static or dynamic typing.

  • @Andersoncarloswoss then, in php 5 has the introduction to typing even if it is only for Arrays and objects with the class name. But the purpose of my answer was to say that it is not a rule and that typing itself is a characteristic of each language.

  • But this is hinttype, has nothing to do with being static or dynamic.

  • @Andersoncarloswoss you’re right, I removed the answer reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.