Re:Try OCaml
by rwmj on Friday September 07, 2018 @06:59AM
(#17933313)
One reason is that promotion from int to float is not free! Therefore maybe it should be explicit that your code is doing that especially in a language like OCaml that cares a lot about performance and is used in a few performance-sensitive areas (notably HFT).
[ Parent
| on HN ]
Re:Try OCaml
by christophilus on Friday September 07, 2018 @08:23AM
(#17933676)
You can have overloaded functions without having type promotions. `1 + 2` would call `addInt`, `1.0 + 2.0` would call `addDouble` and `1 + 2.0` would be a type error, requiring manual conversion in order to fix it.
> promotion from int to float is not free!
Nor is it safe to allow implicit conversion in general, but F# shows that you don’t need separate operators for floating point. You just need a compiler that refuses to implicitly convert data types, as F# does, so you must use explicit conversions and type suffixes if you want to mix data types. If you manually convert, then on your head be the consequences.
This is not legal F#:
let x = 1.0 + 1
But both of these are legal: let x = 1.0 + 1.0
let y = 1.0 + float 1
F# doesn’t even let you mix single- and double-precision FP: let x = 1.0f + 1.0 // ILLEGAL
let x = 1.0f + float32 1 // legal
Re:Try OCaml
by ernst_klim on Sunday September 09, 2018 @07:10PM
(#17948288)
And both F# and sml have a problem with that. What type should this function have:
let add x y = x + y
The proper solution for ad-hoc polymorphism is either dynamic dispatching (aka oop solution) or type-classes or OCaml's modular implicits, not ad-hoc monkey patches like that. And I find (+.) fine since float addition is not an arithmetic addition.> What type should this function have:
If you don't call it, F# simply guesses "int -> int -> int". If you pass a float, you get back a curried function taking float and returning float. Now that the "add" function's type is known, you now can't pass an int: that's now known to be an error.
Seems sensible to me. No problem.