News for hackers. Stuff that matters.
Re:Strong Types and Their Impact on Testing by masklinn on Tuesday November 18, 2014 @02:03PM (#8625640)
> "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…
Re:Strong Types and Their Impact on Testing by djur on Tuesday November 18, 2014 @02:37PM (#8625837)
Your 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.

Re:Strong Types and Their Impact on Testing by tikhonj on Tuesday November 18, 2014 @05:01PM (#8626650)
Haskell 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.
Re:Strong Types and Their Impact on Testing by djur on Tuesday November 18, 2014 @08:53PM (#8627750)
Ah, thanks for the correction.
Re:Strong Types and Their Impact on Testing by michaelochurch on Tuesday November 18, 2014 @09:24PM (#8627866)
(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)
Re:Strong Types and Their Impact on Testing by ForHackernews on Wednesday November 19, 2014 @08:55PM (#8633974)
> True being coercible to a number is unfortunate

If we want to be really pedantic, in Python 2.X, True isn't coercible to an integer, it is an integer.

>>> True == 1 True >>> isinstance(True, int) True

This is an artifact of the fact that when Python was first created, it didn't have boolean types, so when they were added later, "True" and "False" were bolted on as aliases to the integers 1 and 0.

Re:Strong Types and Their Impact on Testing by dllthomas on Tuesday November 18, 2014 @02:25PM (#8625765)
I 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.
Re:Strong Types and Their Impact on Testing by mikeash on Tuesday November 18, 2014 @02:26PM (#8625770)
That'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?
Re:Strong Types and Their Impact on Testing by masklinn on Tuesday November 18, 2014 @02:59PM (#8625961)
> 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.

Re:Strong Types and Their Impact on Testing by mikeash on Tuesday November 18, 2014 @03:12PM (#8626036)
When 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.

Re:Strong Types and Their Impact on Testing by masklinn on Tuesday November 18, 2014 @03:35PM (#8626168)
> 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.

Re:Strong Types and Their Impact on Testing by dllthomas on Tuesday November 18, 2014 @02:27PM (#8625777)
If 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?