English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Same label in different types, how do people solve this?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-12-14 (18:17)
From: Mattias Waldau <mattias.waldau@a...>
Subject: Functions must be explicitly typed, (was Same label in different types, how do people solve this?)
> In Felix, I compromised: functions must be explicitly typed
> when declared, but values don't:
>	function f(a:int): int { val b = a; return b; }

Which I think is an good idea. I normally type the functions in Ocaml, but I
don't type the local variables. Then I will get the type-error message at
the function definition and not at references to the function.

In the following example I forgot the 2nd argument to f2 in g. Now the
compiler will complain when the function g is called from h and point at h
for the error. THIS IS TOO LATE!

let f2 x y = x * y

let g x = f2 x

let h x = 2 * g x
This expression has type int -> int but is here used with type int

However, if I had wrote

let f2 (x:int) (y:int) = x * y

let g (x:int) : int = f2 x
This expression has type int -> int but is here used with type int

let h (x:int) = 2 * g x

I would have gotten a compiler error directly since the type of g isn't int,
it is the closure 'f2 x'. AND NOW I CAN CORRECT THE ERROR DIRECTLY

I don't really see the use of infering types for functions. It works for one
person hacks, like compilers, interpreters, theorem provers, but it doesn't
scale well to programs with many programmers involved.

The types of the function is vital information when you try to understand
the code. And putting this information in a separate file (.mli) for some of
the functions (the public ones) isn't a very good solution.

If you required that the arguments where typed, wouldn't it be possible to
get rid of the distinction of * and *., and infer if it is float or int? It
is a big pain today changing from int to float in a program, in non-ML
languages you just make 'M-x query-replace "int" "float"'