I have been reading about
versus
and I understand that they are different things (as also discussed here) so I have been thinking of example languages in this manner:
So my question is, is there any static-weak typed language out there? (I imagine there would be little point in doing so if not none). And also are my understanding/examples above correct?
C and C++ are considered weakly typed since, due to type-casting, one can interpret a field of a structure that was an integer as a pointer.
Examples of languages with weak typing are bash, awk and PHP. Another kind of weakly typed language is C, where the data at a memory address can be treated as a different type by casting.
JavaScript is considered a “weakly typed” or “untyped” language. The type in question here are the data types—nothing to do with typing from your keyboard.
Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.
The definition of strongly and weakly typed is not well defined, especially in the context of rating just one language. It is a commonly used axis on which to compare languages, and in that context strong and weak typing gain more meaning but it is important to understand that there is no rigorous definition like static and dynamic. What makes a type system weak or strong comes down to a the ways in which the programmer is able to create type errors.
A lot of people would consider C weakly typed because a programmer is allowed to cast types. I can add a pointer to a character if I just tell C that they are both integers.
int main () {
char c = 'a';
void *p;
(int)c + (int)p;
}
In Haskell, however, I can explicitly cast from on type to another, but only certain types will work.
ord('c') + 10
fromIntegral (2::Int) + 4.13
Java has static type casting as well which allows the programmer to, for example, downcast objects. This makes the static type system not sound. But Java has dynamic type checking for just this reason. Yes, Java has dynamic and static type checking. For this reason, however, I think many people would consider Java to be strongly typed.
Perl and Javascript will take strings and consider them to be numbers if they look enough like a number and automatically make it work.
'2 is my favorite number' + 413 == 415 # in Perl
If you want to a string to a number in, say, Scheme you have to explicitly convert using a function that does a check and raises exception if they string is not a number.
(= (+ (string->number '2') 413) 415) ; In Scheme
For this reason a lot of people would consider Scheme strongly typed.
In some languages there aren't any types. The untyped Lambda Calculus is one such example. This is clearly not strongly typed. Everything is a function. I can have numbers using Church Numerals or pairs or strings or whatever using various encodings but values only mean what I agree they mean and there is certainly overlap.
Like I said, the terms are not well defined but they are a little more useful when used in a relative manner. For example I could make a good claim that OCaml is more strongly typed than Java, because Java allows for explicit static down casting whereas OCaml does not.
The terms aren't rigorous but they are useful. To answer your original question, in my opinion, C/C++ are static and weakly typed, so they fit the description.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With