Some corrections
Posted Nov 5, 2007 9:03 UTC (Mon) by
flewellyn (subscriber, #5047)
In reply to:
Many good points by drag
Parent article:
Daniel Bernstein: ten years of qmail security
"Dynamicly typed" always ment to me that the type is determined when the variable is
created, based on various rules. That is variable types do not have to be declared before you can
use them. (although they can be if you prefer it)
The opposite is "staticly typed" were you have to declare variables types before using them.
No. The "dynamic" verus "static" in the typing terms mean solely this: at what time is the
type of this variable known? If it's known at compile-time, the variable is statically typed. If it
can't necessarily be known until runtime, that variable is dynamically typed. Whether or not you
have to declare types ahead of time is mostly irrelevant. I say "mostly" because some languages
that are statically typed have facilities for dynamic typing if you want it, and some dynamically
typed languages can do static typing if you ask for it.
A number of languages, like Haskell and Boo, have static typing, but by default use "type
inference" to determine the type of a variable. So you can declare (using Boo here):
x = 1
And the variable x is determined to be numeric, and an integer. You can't thereafter assign
a string, a float, or a value or object of some other type to x, since it's been determined that the
type of x is integer.
You can, in Boo at least, declare a type, in case you need to do something special, so if I had
said:
x as float = 1
Then x would be a float, and the 1 would be interpreted as 1.0. Also, Boo has optional
"duck typing" that you can use to defer type resolution for a specific variable until runtime. This
is a good idea if you are assigning user input to a value, and don't necessarily know what type
that input will be. (If you DO know, it's a good idea to declare the type, so that the compiler
knows what to do with it.)
On the other hand, some languages that are dynamically typed by default, such as Common
Lisp,
have optional type declarations; when you declare a variable's type, that variable becomes
statically typed, and the compiler is free to leave out the usual type checks, which can improve
performance. Some CL implementations will also treat type declarations as assertions if you set
the compiler's optimization settings a certain way, so that you can get the benefit of static type
checking if you want and need it. (Strictly speaking, a CL implementation is free to ignore type
declarations altogether according to the spec, so this behavior is entirely implementation-
dependent.)
But the crucial point here is that "static" versus "dynamic" typing has everything to do with
WHEN
a type is known, and nothing really to do with HOW it's known.
And "Strongly Typed" has always ment, to me, that once the variable is created then it
can't be changed. That is if you create a variable as a int you can only use it as a int. If you
want to use the int's value as a string you have to create a second string variable. The type
is enforced by the language.
The opposite of that is "weakly typed" were a int can be a string can be a float based on the
context in which it's used. That is if you make a 'int' you can use as a string if you feel
like it. The type is not enforced by the language.
This is closer to correct, but still off. "Strongly typed" means that the VALUE'S type is
strongly enforced: you can't add a string to an integer, or an integer to a character, without
explicit casts, which may not work in any case (how do you coerce "Ich bin ein Berliner" to a
numeric type?). You
can have a strongly-typed dynamic language (Common Lisp), or a weakly typed static language
(C).
The business of whether or not a variable can be rebound to a different type is a matter of
static
versus dynamic typing, not strong versus weak type safety. You can, in Common Lisp, bind a
variable to a string value, then rebind it to a number, or a structure or class object, for that
matter; just don't try to use string functions on the number. THAT'S strongly typed. (On the
other hand, C will not let you assign a string value to an integer variable, but you could treat the
int as a char.)
(
Log in to post comments)