4 ms·
I agree, it would be good to tighten up the terminology. "Weak typing" should be reserved for stuff where the underlying bits can get reinterpreted at will, e.g
by mikeash 12y ago
I agree, it would be good to tighten up the terminology. "Weak typing" should be reserved for stuff where the underlying bits can get reinterpreted at will, e.g.:
printf("%d", 42.0);
Strong typing means the type of each chunk of memory is known and can't be arbitrarily changed, and then static/dynamic refers to how much type information is present at compile time.
Might be a losing battle, though. Strong/weak has been overloaded in this way for a long time.
- masklinn 12y ago> "Weak typing" should be reserved for stuff where the underlying bits can get reinterpreted at will, e.g.: > printf("%d", 42.0); which does not demonstrate your point and is valid Python: >>> "%d" % 42.0 '42' also the more odd >>> "%.2f" % True '1.00' and who could forget >>> "foo" * 3 'foofoofoo' edit: forget about this, for some reason I'd forgotten what the example would output…
- dllthomas 12y agoI think it was meant to be C, where it would often (although not always - very much implementation defined) wind up interpreting the bits of the float as if they represented an integer.
- mikeash 12y agoThat's a C example, where it demonstrates my point perfectly. Python doesn't even have a printf function, where did you get the idea it was supposed to be Python?
- dllthomas 12y agoIf someone is saying something bad about a language and it has something to do with types being weak, they must be saying it about Python?
- masklinn 12y ago> That's a C example, where it demonstrates my point perfectly. Yeah I kinda forgot about the result. > Python doesn't even have a printf function, where did you get the idea it was supposed to be Python? Nowhere? I never assumed your example was python, I was showing something irrelevant.
- mikeash 12y agoWhen you quoted my printf statement and followed it with "which does not demonstrate your point and is valid Python" that kind of implied you thought it a) doesn't demonstrate my point and b) is valid Python. If that's not what you mean, that was not at all evident.
- masklinn 12y ago> If that's not what you mean It's not, I pretty much fell into the issue you decry at the end of your comment: I considered the operation being allowed at all (by the compiler and the runtime) not being evidence of weak typing. Which I guess is technically correct, but is mostly completely irrelevant to the point you were making.
- djur 12y agoYour first Python example is just numeric coercion, not weak typing. Haskell does numeric coercion too (try 2 + 1.1 in ghci). C does not behave like Python and print 42 in this example. Try it yourself and see. True being coercible to a number is unfortunate, but it's still not the same as what C does with that printf. "foo" * 3 is just an example of a polymorphic operator. You could implement the same in any strongly typed language that allows polymorphic operators.
- deleted 12y ago[deleted]
- tikhonj 12y agoHaskell doesn't do numeric conversion, it just allows numeric literals to have any valid numeric type. If you tried it with variables you would get a type error: > let x = 1.0 :: Double > let y = 1 :: Int x + y <interactive>:10:5: Couldn't match expected type ‘Double’ with actual type ‘Int’ In the second argument of ‘(+)’, namely ‘y’ In the expression: x + y It sometimes looks like conversion because the literal `1` is polymorphic and can have a more specific type like `Int` or `Double` inferred from context.
- djur 12y agoAh, thanks for the correction.
- michaelochurch 12y ago(Different poster.) In fact, one of the beautiful things about Haskell is that the type system can (usually) infer exactly as much specificity as is needed. λ> :t 5 5 :: Num a => a λ> :t 5.6 5.6 :: Fractional a => a λ> :t (5 :: Integer) (5 :: Integer) :: Integer The type of the literal "Num a => a" means that the literal "5" works for any type in the Num typeclass. So you can mix literals with any numeric type because the compiler will correctly infer the right type. You can even get usages like this: λ> :t (\x -> x + 5) (\x -> x + 5) :: Num a => a -> a λ> let f = (\x -> x + 5) in (f (0 :: Int), f (0 :: Double)) (5,5.0)
- deleted 12y ago[deleted]
- bunderbunder 12y agoI'm inclined to call that "optionally typed". As in, values nominally have a static type associated with them, but it's possible (even easy) to bypass the type system. C#, for example, is mostly strong and static. But within 'unsafe' contexts you're allowed a lot of those kinds of C-style shenanigans, and you can replicate C unions using the StructLayout and FieldOffset attributes, so the type system is ultimately optional. I think of weak typing as a whole continuum of different things. At an extreme end you have C's level of optional typing. But there's also more tame stuff like implicit casting (which C# also has) that I'd also count as a form of weak typing.
- deleted 12y ago[deleted]
- AnimalMuppet 12y agoWell, printf is a vararg function, so even what type checking C has is out of play (except for the first parameter). C doesn't just let you reinterpret bits at will. You have to jump through one of a few very specific hoops. You have to cast, you have to use a union, or you have to use a vararg function. (Granted, those are some pretty big hoops, but my point is, you have to use one in order to reinterpret bits.) I don't think C's type promotion rules are in the same category. You can promote a short to a double when adding it to a double, but that's not exactly "reinterpreting bits at will".
- mikeash 12y agoOr you can call a function without a prototype (no longer allowed in C99, thankfully), call a function whose prototype's type signature doesn't match the implementation, access a global variable whose declaration doesn't match the definition, access a struct using a different declaration than the one used by whoever put the data there in the first place, or just do an accidental type pun through a void *, which requires no casting. It's not at all hard to do by accident. Much of this is why binary compatibility for libraries written in C is so hard to achieve over the long term. I agree that type promotion isn't the same, but I didn't mention that anyway....
- AnimalMuppet 12y agoArgh. I forgot that C doesn't do name mangling. Most of those problems go away in C++, though (he said hopefully, but without complete confidence...)
- mikeash 12y agoThe problem of mismatched prototypes goes away (except for extern "C" functions, of course) but then you get fun stuff like fragile base classes. If you have a base class in a library and a subclass in an application that links against that library, you can never add a virtual function to the base class again without breaking backwards compatibility (and causing all sorts of accidental type-punning chaos when you try to call methods).