8 ms·
But very, very few languages are 'weakly' typed, to the point of it almost being a silly way to talk about them. Like Perl and PHP? Perhaps few (in number) lan
by kyllo 12y ago
But very, very few languages are 'weakly' typed, to the point of it almost being a silly way to talk about them.
Like Perl and PHP?
Perhaps few (in number) languages are weakly typed, but they're also a few of the most commonly used languages in existence.
- chrisseaton 12y agoThere's no good accepted definition of weak vs strong typing in the literature, but I don't think many people would agree that PHP or Perl are weakly typed. I would say they are both strongly, and dynamically, typed. Can you argue otherwise?
- kyllo 12y agoHere's a trivial example of what I mean by strong vs. weak typing in dynamic languages: Python (strongly typed): >>>1 + "1" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' Ruby (also strongly typed): irb(main):001:0> 1 + "1" TypeError: String can't be coerced into Fixnum from (irb):1:in `+' from (irb):1 from C:/Ruby193/bin/irb:12:in `<main>' PHP (weakly typed): echo 1 + "1"; 2 Perl (also weakly typed): print 1 + "1"; 2
- chrisseaton 12y agoI'd say that was an example of strong typing. The + operator in both languages converts types if required. But it's a proper type conversion - it's not reinterpreting the existing data as another type. If you are happy with a method like Ruby's puts converting objects to a string, why is + any different? They both do type conversion if required. In my mind an example of a weakly typed language would look like this: print 1 + "1"; > 399299293 Here my imaginary language has taken the object that represents the string "1", it has ignored the type and has reinterpreted the string object (which happens to be a pointer to a character array) as if it were an integer. This is what C does - which is why it is a weakly typed language in my mind. But as I say, the are no good definitions.
- sanderjd 12y agoI think your definition is "good", even if it isn't widely-agreed-upon. There may be other "good" definitions, but I don't think the one your parent post is one, because it doesn't seem to permit a "strongly typed" language to define methods that take different types and convert them before operating on them, which would make all languages "weakly typed".
- imanaccount247 12y ago>The + operator in both languages converts types if required. But that is what weak typing is. >In my mind an example of a weakly typed language would look like this Just because it is converting the other way doesn't mean it is a different thing. The fact that it one happens to convert in the direction that pleases you doesn't mean it is "strong" now. If it is converting, then it is weak.
- chrisseaton 12y agoBut most languages have some operation that will implicitly convert values to a different type. In fact Ruby and Python both do this (puts and print are examples), and they were examples given of a strongly typed language.
- imanaccount247 12y agoI didn't give those examples. Strong/weak isn't binary, it is a scale. A language that does more implicit conversion is weaker than one that does less.
- cbd1984 12y agoIt's the difference between conversion and casting: Casting, explicit or implicit, throws away type information, and reinterprets the same data in a different way. Data, here, is bytes, or some other low-level representation that makes what the types apply to. Conversions, explicit or implicit, uses type information to either change data, or prove that no change is needed, in order to use the data a different way.
- klibertp 12y ago"weak typing" usually means a type system (static or dynamic) where there are many implicit coercions going on. "strong typing" requires these casts to be written explicitly. The only languages I know which would fit your definition of "weak typing" are Forth and Assembly (no implicit casts ever, no type checking at all). Maybe TCL, but I'm not sure. Anyway, using your definition, every other language would become "strongly typed". And this wouldn't be very practical, I think, because then we'd have no way of distinguishing type systems of for example PERL and Python.
- cbd1984 12y ago> "weak typing" usually means a type system (static or dynamic) where there are many implicit coercions going on. "strong typing" requires these casts to be written explicitly. This makes no sense, because both of those definitions assume the runtime and/or compiler has the same amount of type information available to it, and the only difference is what the runtime and/or compiler chooses to do with it. Weakly typed languages have less type information, either because they never had it or because they allowed the programmer to throw it away. Casts and conversions are not the same thing: A cast throws away type information so the same data can be used in a different way, a conversion uses the type information to change the data (or prove no change is needed) in order to use it in a different way. C has casts, Javascript has conversions. C is weakly typed, Javascript is strongly dynamically typed.
- kyllo 12y agoI think the appropriate term for languages like FORTH and assembly is "untyped."