6 ms·
Doesn't all modern programming languages has Loose typing? It allows more abstraction. Like for example in JavaScript where a object can be anything!
by z3t4 12y ago
Doesn't all modern programming languages has Loose typing? It allows more abstraction. Like for example in JavaScript where a object can be anything!
- AnimalMuppet 12y agoNo. Some (more or less) modern languages have loose typing. Some modern languages have quite strict typing (Haskell for instance, but there are others).
- steveklabnik 12y ago'loose' typing isn't a word normally used when talking about type systems. Most people work with a two-axes "strong" vs "weak" and "dynamic" vs "static" system. But very, very few languages are 'weakly' typed, to the point of it almost being a silly way to talk about them. Ruby, for example, is Dynamic and Strong. Then there's the 'types' vs 'tags' issue...
- kyllo 12y agoBut 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.
- 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."
- jholman 12y ago> Most people work with a two-axes "strong" vs "weak" and "dynamic" vs "static" system. I dispute this. First of all, I think that most programmers use a one-axis system of strong/weak, where "strong" means approximately "the safety that I like", and "weak" means approximately "lacking the safety that I like", a system in which they have no words for "additional safety that I do not value". I'm not aware of any useful definition of these words, nor any specific definition widely agreed upon by any meaningful non-partisan class of speakers (e.g. "working programmers" or "published academic type theorists"). Can you correct me? As such, I think that saying "very, very few languages are 'weakly' typed" is unlikely to be understood by your audience in the sense that you intended it (whatever sense that was). I agree that the dynamic/static axis seems to be in wide use, and I'm unaware of massive divergences in usage. Although note that many people spell the word "static" using the letters ['s', 't', 'r', 'o', 'n', 'g'].
- sanderjd 12y agoI seem to be inside your parent's "most people" and you seem to be outside of it. I have no idea if the word "most" is appropriate, but at least "some" is. The sense in which that post intended "weakly typed" is the one in which it means that types are checked neither at compile-time (statically) or at runtime (dynamically). For instance, (most?) assembly is "weakly" typed, because I can "pass" whatever data I want into a subroutine without it noticing whether or not that data has the correct type. On the other hand, Ruby is "strongly" typed, because I get "NoMethodError" if I try to call a method on a type that doesn't implement that method. And Rust is also "strongly" typed because I get an error from the compiler if I try to call a method on a type that doesn't implement that method. But Ruby is "dynamically" typed because I get that "NoMethodError" at runtime, while Rust is "statically" typed because I get that compiler error at compile-time. I guess you know all this stuff, but you seem to think the terminology is less common than I do!
- jholman 12y agoYeah. And all three of us are outside of my "most programmers", which is unsurprising, because we are reading comments on a post about type systems, which is a weird thing to do. I encourage you to go ask some random C++ programmers, perhaps a bunch of EA employees, whether C or PHP is more "strongly typed", and see what they say. Note that because of void pointers, I believe that C fits your definition of "weakly typed", and as far as I know, PHP does not fit your definition (and btw I agree with you about what steveklabnik intended, though we may both be wrong). And yeah, we're primarily disagreeing about what classes of people agree on what terminology. This is why I agree with chrisseaton in the cousin-thread, that there is no good definition... people assume other people know what "strong" means, and yet they mostly disagree. And I'd like to repeat that as far as I can tell, if you survey the type-theorist literature, I think that type theorists a) have mostly stopped using those stupid vague labels, and b) back when they did use them, each author used them for something different and repeat my entreaty for evidence to the contrary.
- kazinator 12y ago"A variable can be anything" isn't loose typing. It's just that the type expressions that describe the program are not (necessarily) compile-time constant expressions. In many "variable can be anything" languages, you cannot for instance add the character string "3" to the number 3 to obtain 6. You get an error, like "the string '3' is not an arithmetic operand". So even though the variable which holds that "3" can be anything, the arithmetic add operation doesn't take any kind of value. Languages of this type can be subject to static checking, which can identify potential programs. Mature dynamic languages have good compilers with such checks. Instead of being told "you cannot run this program because of such and such a type mismatch", you get a softer diagnostic like "there is probably a type mismatch here". (And if the program is run anyway, the mismatch is still caught at run time and turned into an exception---if the execution of the program steps into the code with the values which trigger the problem.)
- mkehrt 12y agoHaskell, Scala, Rust, Java (you can't really consider 8 to be not modern), even C++11. Just off the top of my head. Thank God.