Add type aliases for type expressions
This commit is contained in:
1 parent
7a286f2e06
commit
d82a9bb930
6 files changed
+227
-1
No files matched your search
@@ -32,6 +32,20 @@ Generic types are invariant by default. You can specify declaration-site varianc
|
||||
|
||||
`out` makes the type covariant (produced), `in` makes it contravariant (consumed).
|
||||
|
||||
# Type aliases
|
||||
|
||||
Type aliases name type expressions (including unions/intersections):
|
||||
|
||||
type Num = Int | Real
|
||||
type AB = A & B
|
||||
|
||||
Aliases can be generic and can use bounds and defaults:
|
||||
|
||||
type Maybe<T> = T?
|
||||
type IntList<T: Int> = List<T>
|
||||
|
||||
Aliases expand to their underlying type expressions. They can be used anywhere a type expression is expected.
|
||||
|
||||
# Inference rules
|
||||
|
||||
- Literals set obvious types (`1` is `Int`, `1.0` is `Real`, etc.).
|
||||
|
||||
@@ -467,6 +467,13 @@ It cannot be combined with an explicit type annotation.
|
||||
class A(x?) { ... } // x: Object?
|
||||
fun f(x?) { x == null } // x: Object?
|
||||
|
||||
Type aliases name type expressions and can be generic:
|
||||
|
||||
type Num = Int | Real
|
||||
type Maybe<T> = T?
|
||||
|
||||
Aliases expand to their underlying type expressions. See `docs/generics.md` for details.
|
||||
|
||||
`void` is a singleton value of the class `Void`. `Void` can be used as an explicit return type:
|
||||
|
||||
fun log(msg): Void { println(msg); void }
|
||||
@@ -503,6 +510,8 @@ Empty list/map literals default to `List<Object>` and `Map<Object,Object>` until
|
||||
val xs = [] // List<Object>
|
||||
val ys: List<Int> = [] // List<Int>
|
||||
|
||||
Map literals infer key/value types from entries; named keys are `String`. See `docs/generics.md` for details.
|
||||
|
||||
## Flow analysis
|
||||
|
||||
Lyng uses flow analysis to narrow types inside branches:
|
||||
|
||||
Reference in new issue
Block a user