An example is Crystal; [edited] example from the website:
if rand(2) > 0
my_string = "hello world"
end
puts my_string.upcase
Execution:
$ crystal hello_world.cr
Error in hello_world.cr:4: undefined method 'upcase' for Nil (compile-time type is (String | Nil))Languages like Haskell, OCaml, F#, and Elm use global type inference.
Languages like Scala and TypeScript use local type inference. TypeScript in particular uses flowed type inference (there might be a more formal name for this). For example:
let a = document.querySelector('#a') // typeof a is Element | null
if (a == null) throw 'not found' // typeof a is now Element
[0] https://en.wikipedia.org/wiki/Type_inference[1] https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_sy...
But gradual typing means avoiding typing alltogether in some parts of the program and in some sense going against the inferred type. This is probably most relevant in Haskell where IO is typed, and less so in others, e.g Clojure.
If you want to learn more about this, the Damas Milner unification-based algorithm used in ML, Haskell and others is a must-read starting point. It does global type inference of all types in the program without requiring any annotations, by analyzing how the types are used. It also can detect when a variable is only passed around, without being used in primitive operations, in which case it infers a generic/parametric type for it.
let f(x, y) = x#foo(y#bar(x))
`#` is the member access operator. So this is a function that takes two objects (strictly speaking, it takes a single tuple of two objects), calls the method `bar` on `y` passing `x` as argument, then calls the method `foo` on `x` passing the result of the previous call as argument, and finally returns the result of that. Nowhere are the types declared, but they are fully inferred and statically checked. The actual type of `f` is rather convoluted when written out fully: (<foo: 'b -> 'c; ..> as 'a) * (<bar: 'a -> 'b; ..>) -> 'c
The `<>` notation means "object", with members listed inside delimited by semicolons. `foo: 'b -> 'c` means that there must be a member named "foo", which is a function that takes a single argument of some type 'b, and returns a value of some other type 'c - these are like template type parameters in C++, except you don't have to declare them. Finally, `..` means "and any other members" - without it, our function would only accept objects that have only the member named "foo" with the designated signature. So, `<foo: 'b -> 'c; ..>` means "any object that has method "foo" that takes a value and returns a value, and also possibly some other members". The `as 'a` part then assigns another type parameter to describe that object for reference later.Then we see the object type for the second function argument, which is `<bar: 'a -> 'b; ..>`. This is structured in the same way, but note that it uses type parameters with the same names as before - this indicates that they must be the same type. This makes it possible to make the definitions recursive; the entire bit before `-> 'c` at the end can be described in a more verbose fashion like so:
"A tuple, the first element of which is an object, with method "foo", taking an argument of the same type that is returned by method "bar" of the second element of the tuple, and returning a value of any type, and possibly some other members; and the second element of which is an object, with method "bar", taking an argument of the same type as the first element, and returning a value of the same type that method "foo" takes as an argument, and possibly some other members."
Note that you don't actually need to write any of this. You can explicitly declare the type of "f" if you want to, but the compiler can figure it all out by itself, just by looking at the body, and will print out the type for you. It will also statically verify how the function is used. So, this is okay:
let a_foo = object
method foo y = y + 1
end;;
let a_bar = object
method bar x = x#foo 0
end;;
f(a_foo, a_bar)
But on the other hand, this: f(a_bar, a_foo)
gives a compile-time error for the first argument: This expression has type < bar : < foo : int -> 'a; .. > -> 'a >
but an expression was expected of type < foo : 'b -> 'c; .. >
The first object type has no method foo
And this: f(a_foo, a_foo)
gives an error for the second: This expression has type < foo : int -> int >
but an expression was expected of type < bar : < foo : int -> int > -> int; .. >
The first object type has no method bar
(OCaml also has classes, but their sole purpose is to provide a common implementation for objects, and to allow code reuse and extensibility via inheritance; classes aren't types, and class inheritance does not necessarily denote subtyping.)Effectively, this is static duck typing. This all looks a great deal like C++ templates, in that you can "just do" things, and the validity is effectively determined at the point of use. However, because it is captured at the type system level, the point of use doesn't have to see the implementation of your "template" to typecheck it - it just checks the arguments against the type signature that it computed when the "template" was defined. This also means that `f` is an actual function - not really a template - and so it can be passed around, stored in a variable etc (which in C++ is only possible for specific instantiations of a template).
It is basically equivalent to an alias analysis with imperative assignments (subtypes) and fields (type parameters).
Also, I believe the work you are citing above explores structural subtypes, not nominal ones.
pair x y = (x, y)
This function creates a pair for any two types, and doesn't require any annotations. >>> x = 1
>>> type(x)
<class 'int'>
>>> x = 'foo'
>>> type(x)
<class 'str'>Also, I'm not sure how a union type could be distinguished from an error during type inference. Maybe a nullable, but otherwise it'd be very tricky.
Even languages with overloading dont allow them in the same namespace if they also have the same signature.
It is also possible for the compiler to use type information to attempt to disambiguate. Especially because this would normally happen across module boundaries, a hypothetical compiler could:
1) Infer types based on all non-ambiguous references (or all references within the compilation unit that contains the definition)
2) Disambiguate all ambiguous reference using type information.
I suppose a sufficiently smart compiler could attempt to do type inference and disambiguation at the same time; but that sounds like introducing NP hard problems to the compiler for the sole purpose of allowing confusing code.
Edit: example to further clarify what I meant (it's a bit more than type inference at initial assignment)