You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, An Ocaml beginner here, what does it mean to have _ in your type signatures ? like
val id2 : '_a -> '_a = <fun>
IwanKaramazow - Today at 12:25 AM
A type variable '_a (the actual letter doesn't matter, the crucial thing is the underscore) is a so called weak type variable. This is a variable that cannot be generalized, i.e., it can be substituted with only one concrete type. It is like a mutable value, but in the realm of types.
A type denoted with a weak type variable '_a is not included in a type that is denoted with a generic type variable. Moreover, it even can't escape the compilation unit, and should be either hidden or concretized.
Weak type variables are created when an expression is not a pure value (that is defined syntactically). Usually, it is either a function application, or an abstraction. It is usually possible, to get rid of weak type variables by doing a so called eta-expansion, when you substitute a partially applied function to a normal function application by enumerating all function arguments
i.e. id3 = ( fun x -> id2 x)
FWIW, I would probably use a message like this, given the following:
val f : '_a -> '_b
The type signature of f includes weakly polymorphic type variables ('_a, '_b) so f may not be as polymorphic as expected. Polymorphic generalization usually only occurs if f is defined as a syntactic value. Consider rewriting f using an explicit fun to make it into a value.
cc @IwanKaramazow
The text was updated successfully, but these errors were encountered: