On Friday, 28 June 2013 at 13:44:19 UTC, ponce wrote:
On Friday, 28 June 2013 at 00:34:54 UTC, JS wrote:
Would it be possible for a language(specifically d) to have
the ability to automatically type a variable by looking at its
use cases without adding too much complexity?
Well ocaml has it
(https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner) and well
it's not all that positive, at least in that language.
No, OCaml doesn't quite do what the OP is asking for. In
particular, the part where x is assigned an int and subsequently
assigned a float can not be modeled directly. Even the separate
'auto x' and subsequent use of x is different from how OCaml's
let polymorphism works. To model that, you could declare
everything as 'a option refs and assign them later, like
let main () =
let x = ref None in
let y = ref None in
begin
x := Some 1;
y := Some (f());
end
but if you want to use x as another type later you'll have to
shadow the declaration in a new scope.
Combined with parametric polymorphism it's nice and sound,
except it has the potential to hide a simple typo a lot further
from where it is (think mixing up integer operators and FP
operators).
As it break overloading on "leaves", it is the reason ocaml has
"print_string" and "print_int" which is quite frankly ugly.
Yes, and the +., -., *., /. operators are ugly too, as is the
requirement that record field labels in the same scope be
distinct. Overloading can be dangerous, but no overloading is a
PITA. Combining type inference and overloading is problematic.
Once you have type inference, the first thing you do to make
your code readable is to add back type annotations.
There should always be a .mli file with exported decls. I wish
OCaml had some ability to have separate type decls from the
value, like Haskell. Type inference is most useful inside a
function, IMO.
I think that even if the feature being requested were feasible,
it would be awful for D.
-- Brian