Typing Types
by Zef Hemel- Published:March 28th, 2004
- Comments:No Comment
- Category:General
Yesterday I had a brief discussion about programming languages and type systems and I realized I mixed up static, dynamic, weak and strong typing. I had never really given the differences much thought. I’m going to now, though.
As an example I’ll start with PHP, most of you know PHP, presumably (if not, the example shown work the same in Perl). Some people think it’s an untyped language; that’s not true, it has types, the fact that there’s a gettype function says enough. However is it statically, dynamically, weakly or strongly typed, or a combination of those? Let’s look at this tiny example:
$x = 5; $y = 'Zef'; $z1 = $x . $y;
What happens here? At the first line we define a variable $x (assume this is the whole program, so $x wasn’t defined before), what will be the type of $x? We don’t know, but we can obtain this by using the type of the value assigned to it, which is an integer in this case. If typing takes place in this way, we call it dynamic typing. The type of a value is determined by the value itself, not by the type of the variable it’s assigned to (which would be impossible in PHP because it doesn’t let you define types for variables).
The same happens in the second line, we define a variable $y, type unknown so far. The value assigned to it is a string so $y will be of the type string. You can check this by running
echo gettype($somevar);
After each line (replacing $somevar by $x or $y or other variable).
On the third line we concatenate two variables. Concatenation assumes two strings, but the left operand clearly is not a string, still it concatenates. This is a property of a weakly typed language. In weakly typed languages operations can be performed on operands (variables, values) of the wrong type (in which they are then converted).
Now let’s have a look at a similar Java example:
int x = 5; String y = "Zef"; String z = x - y;
Let’s have a look at the first line. Here we define a new variable called x of the type int. The type of a variable is known at compile time. This is a property of a statically typed language. As x is of type int, we also expect the value assigned to it to be of that type and it is, if it weren’t we’d end up with a compilation error (or in some cases minor changes are made to the value, if a float is expected and we get an int, the value is usually converted to a float).
On the second line we define another variable y of the type String. We expect a value of type String as a value assigned to it, and luckily, that’s what we get.
Then the third line. We define a variable z of type String. We expect a value of type String assigned to it, but we never come that far. When we try to compile this we get this error:
Test.java:5: operator - cannot be applied to int,java.lang.String
String z = x - "Zef";
^
‘-’ is not an operator that can be applied to operands of the type int and String. This means that the language is strongly typed. If we would’ve tried something similar in PHP it wouldn’t have stopped us, it would probably have converted “Zef” to a number, probably 0 (which is obviously a bug in PHP, “Zef” should come close to infinity ;-)) and substracted those (result: 5).
In summary:
- Static typing: verifies the types at compile time
- Dynamic typing: verifies the types at run time
- Strong typing: does not allow operations to be performed on arguments of the wrong type
- Weak typing: allows such operations
On this Wikpedia page (where there’s a lot more information about this subject) we can also find the advantages and disadvantages of statically vs dynamically typing (on which many PHP/Perl/Python vs Java/C#/C++ discussions are based on):
The choice between static and dynamic typing requires some trade-offs.
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce machine code that just does the right thing. Further, compilers in statically typed languages can find shortcuts more easily. See optimization.
Static typing provides documentation for a program, by way of the types.
Static typing allows construction of libraries which cannot be easily accidentally misused by their users. This can be used as an additional mechanism for communicating the intentions of the library developer.
Static typing finds type errors reliably and at compile time, which should increase the reliability of the program. The value of this is unclear, and depends on how frequently type errors make it through a normal software development process compared to other sorts of errors. Static typing advocates feel like programs are more reliable when they have been type checked, while dynamic typing advocates point to distributed code that has proven reliable and to bug databases. The value of static typing increases when the strength of the type system is increased.
A static type system constraints the use of powerful language constructs more than less powerful ones. This makes powerful constructs harder to use, and thus places the burden of choosing the “right tool for the problem” correctly on the shoulders of the programmer, who might otherwise be inclined to use the most powerful tool available. Choosing overly powerful tools may cause additional performance, reliability or correctness problems, because there are theoretical limits on what kind of properties can be expected from powerful language constructs. For example, indiscriminate use of recursion or global variables cause well-documented adverse effects.
Dynamic typing allows constructs that would be illegal in some static type systems. For example, eval functions that execute arbitrary data as code is possible. Furthermore, dynamic typing accommodates transitional code, such as allowing a string to be used in place of a data structure.
Dynamic typing allows debuggers to be more functional; in particular, the debugger can modify the code arbitrarily and let the program continue to run. Programmers in dynamic languages often “program in the debugger” and thus have a shorter edit-compile-test-debug cycle. The need to use debuggers is sometimes considered as a sign of design or development process problems.
Dynamic typing allows compilers to run more quickly, since there is less checking to perform and less code to revisit when something changes. This as well shrinks the edit-compile-test-debug cycle.


Nessun Commento
No comments yet.