Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a statically weak typed language?

I have been reading about

  • static (types are checked at compile time) and
  • dynamic (types are checked at runtime) types

versus

  • strong (no implicit conversion) and
  • weak (implicit conversion) types

and I understand that they are different things (as also discussed here) so I have been thinking of example languages in this manner:

  • static - strong (C, C++, Java, ..)
  • static - weak (???)
  • dynamic - strong (python, ruby, ..)
  • dynamic - weak (perl, javascript, ..)

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?

like image 200
none Avatar asked Dec 26 '12 21:12

none


People also ask

Is C++ weakly or strongly typed?

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.

Is bash a weakly typed language?

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.

What language is weakly typed?

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.

Is Python weakly or strongly typed?

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.


1 Answers

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.

Unchecked explicit casting

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.

Automatic casting

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.

No types at all

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.

Comparison

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.

Conclusion

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.

like image 108
mmachenry Avatar answered Oct 05 '22 13:10

mmachenry