News for hackers. Stuff that matters.
Re:Try OCaml by k__ on Friday September 07, 2018 @06:54AM (#17933293)
Does this mean, it has a special function/operator for adding ints to floats?
Re:Try OCaml by masklinn on Friday September 07, 2018 @06:58AM (#17933309)
It doesn't have any way to add ints to floats directly. You have to convert one to the other then perform the addition.

Haskell or Rust are the same incidentally: while the same "lexical" operator is used all the time (`+`), numeric additions are defined for two values of the exact same concrete type e.g. in Haskell you can add two Int or to Float or two Integer, but not an Int and an Integer, or an Int and a Float:

    Prelude> (1::Int) + (1::Int)
    2
    Prelude> (1::Integer) + (1::Integer)
    2
    Prelude> (1::Float) + (1::Float)
    2.0
    <interactive>:6:15: error:
        • Couldn't match expected type ‘Float’ with actual type ‘Int’
        • In the second argument of ‘(+)’, namely ‘(1 :: Int)’
          In the expression: (1 :: Float) + (1 :: Int)
          In an equation for ‘it’: it = (1 :: Float) + (1 :: Int)
Re:Try OCaml by watshn on Friday September 07, 2018 @01:39PM (#17936147)
While true, that issue arose because you restricted their types. Without the type annotations it would have worked just fine, because the (+) function is polymorphic in the Num typeclass, and because literals are overloaded such that the literal 1 will have type Fractional a when type inference calls for it.

So in ghci:

  Prelude> let x = 1 + 1.0
  Prelude> :t x
  x :: Fractional a => a
  Prelude> x
  2.0
Re:Try OCaml by masklinn on Friday September 07, 2018 @02:14PM (#17936436)
> While true, that issue arose because you restricted their types.

What I was intending to show occurred because I engineered the example to show it, yes, that was the entire point.

> Without the type annotations it would have worked just fine

Without the type annotation it would have been in an actual codebase rather than the REPL, and that codebase would have been typed.

> because the (+) function is polymorphic in the Num typeclass, and because literals are overloaded such that the literal 1 will have type Fractional a when type inference calls for it.

Which does not contradict my point at all.

> So in ghci:

Which works because (drumroll) both operands are of the same Fractional type. Which is the entire point of my comment: Haskell does not allow adding numbers of different types with one another any more than OCaml does.

Re:Try OCaml by RussianCow on Friday September 07, 2018 @10:51AM (#17934665)
It has a separate operator for adding two floats: +.