16

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?

Community
  • 1
  • 1
none
  • 11,793
  • 9
  • 51
  • 87
  • 7
    C may be considered a statically typed language with weak types because of its `void *` and loose type casting. But this is not a good example, I know. – Dmytro Sirenko Dec 26 '12 at 21:29
  • @EarlGray Good think you brought it up since second answer in the linked question argues the same thing. Although you can cast many types to one another, you usually need to be explicit about it so I don't quite see how C can be count as a weak typed language. – none Dec 26 '12 at 21:55
  • 2
    I doubt the primitive types in C/C++/Java should be called strongly typed. Isn't `int xx = 1; char yy = 'b'; float zz = xx + yy;` perfectly valid in all three of those languages? If there are types in a language that are weakly typed, then that makes your whole language weakly typed. –  Dec 27 '12 at 00:11
  • @Tinctorius I see your point. They are weak typed languages if you consider primitives only, but if you think about `string` (excluding C) things becomes different than other weak languages like perl or javascript (e.g cannot add `string` to an `int`). Also as far as I understand there is not even an option for explicit casting in perl hence I guess it's fair to think of it as a weak language whereas C family would be stronger than perl but not as strong as say something like haskell. – none Dec 27 '12 at 01:15
  • 4
    As you already noticed, it depends a lot on your definition of weak typing. Same for almost every other question involving that term. That's why I *hate* that term. I believe we should [abandon the term "weak typing" entirely](http://stackoverflow.com/a/9929697/395760). If you really care about implicit conversions specifically, then you can salvage the question by removing all uses of that term and just speak of implicit conversions instead. –  Dec 27 '12 at 04:41
  • VB, particularly old COM VB. – RBarryYoung Oct 31 '13 at 04:27

2 Answers2

6

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.

mmachenry
  • 1,773
  • 3
  • 22
  • 38
  • `because Java allows for explicit static down casting whereas OCaml does not` I doubt this statement. OCaml also can casts between `float` and `int`(e.g.), just that it carries out that via a function `int_of_float` and `float_of_int`, not like `(int)1.2`. But if you think `(int)` bit as a static method inside Java, then it is the same. I believe you miss used `explicit`, it actually should be `implicit`. For example, in java you can do `1.5 + 1`, the `1` inside is implicitly casted to float, but in OCaml, you can't do that. There is no implicit type cast in OCaml and so it is stronger. – Jackson Tale Aug 22 '14 at 16:50
  • It's true. OCaml does not allow down casting and Java does. The difference here is that what you described in OCaml, int_of_float, is not a down cast. It's a conversion from one type to another. It's not a cast at all. Converting from int to float in Java is also not. Down casting is an operation between subtypes, objects that inherit from one another. I can consider a circle to be a shape in OCaml and in Java. But only in Java can I say, "Hey this shape... it's a circle, trust me." and it'll work statically but get caught by Java's dynamic type checker at runtime if you were incorrect. – mmachenry Aug 22 '17 at 20:59
0

C# allows for user-defined implicit conversions, so it would fit your definition of "static" and "weak" (although the "weakness" is bounded).

Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
  • 1
    that is not what makes a language weakly typed. C# is very strongly typed language. OP's words are not very accurate – nawfal Jan 12 '14 at 21:33
  • 1
    @nawfal - "weak" and "strong" don't have universally agreed upon definitions, and I was going by the one that the OP used. If C# is "very strongly typed" then is F# "very very strongly typed" while Haskell is "very, very, very strongly typed"? Also, it doesn't make sense to talk about the "accuracy" of a definition, people are allowed to make up their own definitions of things (we do it all the time in mathematics), but it's up to you to decide whether or not the definition is useful. – Rodrick Chapman Jan 13 '14 at 02:06
  • I guessed you would be getting into this :) In any case your last comment is a much more useful answer. I didn't vote it for being wrong answer (there is no absolute wrong in a relative point), but for being poor, incomplete answer. If the whole point of strong-ness and weak-ness is contentious, then it deserves a mention. Even more so when the very implicit conversion you're talking about is already a feature of C++ and Java which OP already includes in strongly typed languages, which makes a further mention of a similar language like C# pointless. – nawfal Jan 13 '14 at 02:35
  • All I mean is your answer require a bit more explanation, not because its just incomplete, but because otherwise it can be downright misleading. Personally I dont get this whole no-universal-definition-bandwagon. I can understand when someone says its relative for technical purity, but there is a certain limit around which a language can be classified as strongly typed or weakly typed. The very fundamental nature of languages like php, perl etc are different from languages like c#, java etc. – nawfal Jan 13 '14 at 02:43
  • @nawfal - It's not that the strong/weak dichotomy is contentious, it's that it's context dependent. The C# type system is "weaker" than the F# type system because, for instance, it allows implicit conversions from a type `T` to a type `I` if `I` is an interface that `T` implements. So, saying that a language is weakly-typed is like saying that cars are slow-moving because they must stop at stop-signs while planes don't. – Rodrick Chapman Jan 13 '14 at 03:40
  • Yes its like saying that, but when the spectrum of movable objects is well defined, it has a meaning. If the objects in consideration range from bicycles to rockets, cars are pretty slow. Why? The very nature of cars are to stop at signs, and planes not to. That is how they are designed, that is what their fundamental nature is. Now picking an edge case where a plane had to stop at some airport signal is not significant enough to reason against the fact that they are very very fast. – nawfal Jan 13 '14 at 04:01
  • The very nature of C# and php is different, hence can be classified, but a due explanation of the context is welcome anyway, which is also missing in your answer. One, C# never fits his definition because his definition of strongly typed languages are already C, C++ and Java. Two, for this question if you really mean to answer in a context dependent way, it deserve a mention. – nawfal Jan 13 '14 at 04:07
  • In your example, I cannot conclude that cars are slow without some context. If I'm commuting a few miles between home and work, then the car is probably faster. Even if I'm traveling a very long distance, the car might still be faster if the plane cannot carry enough of my stuff in one trip. Then, of course, there is the matter of efficiency: the difference in transporting `n` people vs. `n + 1` people might be negligible in a car, but significant in a plane. – Rodrick Chapman Jan 13 '14 at 04:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45082/discussion-between-nawfal-and-rodrick-chapman) – nawfal Jan 13 '14 at 04:13