Add generics docs and link from tutorial
This commit is contained in:
parent
40de53f688
commit
de9ac14e33
107
docs/generics.md
Normal file
107
docs/generics.md
Normal file
@ -0,0 +1,107 @@
|
||||
# Generics and type expressions
|
||||
|
||||
This document covers generics, bounds, unions/intersections, and the rules for type expressions in Lyng.
|
||||
|
||||
# Generic parameters
|
||||
|
||||
Declare type parameters with `<...>` on functions and classes:
|
||||
|
||||
fun id<T>(x: T): T = x
|
||||
class Box<T>(val value: T)
|
||||
|
||||
Type arguments are usually inferred at call sites:
|
||||
|
||||
val b = Box(10) // Box<Int>
|
||||
val s = id("ok") // T is String
|
||||
|
||||
# Bounds
|
||||
|
||||
Use `:` to set bounds. Bounds may be unions (`|`) or intersections (`&`):
|
||||
|
||||
fun sum<T: Int | Real>(x: T, y: T) = x + y
|
||||
class Named<T: Iterable & Comparable>(val data: T)
|
||||
|
||||
Bounds are checked at compile time. For union bounds, the argument must fit at least one option. For intersection bounds, it must fit all options.
|
||||
|
||||
# Variance
|
||||
|
||||
Generic types are invariant by default. You can specify declaration-site variance:
|
||||
|
||||
class Source<out T>(val value: T)
|
||||
class Sink<in T> { fun accept(x: T) { ... } }
|
||||
|
||||
`out` makes the type covariant (produced), `in` makes it contravariant (consumed).
|
||||
|
||||
# Inference rules
|
||||
|
||||
- Literals set obvious types (`1` is `Int`, `1.0` is `Real`, etc.).
|
||||
- Empty list literals default to `List<Object>` unless constrained by context.
|
||||
- Non-empty list literals infer element type as a union of element types.
|
||||
|
||||
Examples:
|
||||
|
||||
val a = [1, 2, 3] // List<Int>
|
||||
val b = [1, "two", true] // List<Int | String | Bool>
|
||||
val c: List<Int> = [] // List<Int>
|
||||
|
||||
Spreads propagate element type when possible:
|
||||
|
||||
val base = [1, 2]
|
||||
val mix = [...base, 3] // List<Int>
|
||||
|
||||
# Type expressions
|
||||
|
||||
Type expressions include simple types, generics, unions, and intersections:
|
||||
|
||||
Int
|
||||
List<String>
|
||||
Int | String
|
||||
Iterable & Comparable
|
||||
|
||||
These type expressions can appear in casts and `is` checks.
|
||||
|
||||
# `is`, `in`, and `==` with type expressions
|
||||
|
||||
There are two categories of `is` checks:
|
||||
|
||||
1) Value checks: `x is T`
|
||||
- `x` is a value, `T` is a type expression.
|
||||
- This is a runtime instance check.
|
||||
|
||||
2) Type checks: `T1 is T2`
|
||||
- both sides are type expressions (class objects or unions/intersections).
|
||||
- This is a *type-subset* check: every value of `T1` must fit in `T2`.
|
||||
|
||||
Exact type expression equality uses `==` and is structural (union/intersection order does not matter).
|
||||
|
||||
Includes checks use `in` with type expressions:
|
||||
|
||||
A in T
|
||||
|
||||
This means `A` is a subset of `T` (the same relation as `A is T`).
|
||||
|
||||
Examples (T = A | B):
|
||||
|
||||
T == A // false
|
||||
T is A // false
|
||||
A in T // true
|
||||
B in T // true
|
||||
T is A | B // true
|
||||
|
||||
# Practical examples
|
||||
|
||||
fun acceptInts<T: Int>(xs: List<T>) { }
|
||||
acceptInts([1, 2, 3])
|
||||
// acceptInts([1, "a"]) -> compile-time error
|
||||
|
||||
fun f<T>(list: List<T>) {
|
||||
assert( T is Int | String | Bool )
|
||||
assert( !(T is Int) )
|
||||
assert( Int in T )
|
||||
}
|
||||
f([1, "two", true])
|
||||
|
||||
# Notes
|
||||
|
||||
- `T` is reified as a type expression when needed (e.g., union/intersection). When it is a single class, `T` is that class object.
|
||||
- Type expression checks are compile-time where possible; runtime checks only happen for `is` on values and explicit casts.
|
||||
@ -10,6 +10,7 @@ __Other documents to read__ maybe after this one:
|
||||
- [OOP notes](OOP.md), [exception handling](exceptions_handling.md)
|
||||
- [math in Lyng](math.md), [the `when` statement](when.md), [return statement](return_statement.md)
|
||||
- [Testing and Assertions](Testing.md)
|
||||
- [Generics and type expressions](generics.md)
|
||||
- [time](time.md) and [parallelism](parallelism.md)
|
||||
- [parallelism] - multithreaded code, coroutines, etc.
|
||||
- Some class
|
||||
@ -555,6 +556,8 @@ Type arguments are usually inferred from call sites:
|
||||
val b = Box(10) // Box<Int>
|
||||
val s = id("ok") // T is String
|
||||
|
||||
See [Generics and type expressions](generics.md) for bounds, unions/intersections, and type-checking rules.
|
||||
|
||||
## Variance
|
||||
|
||||
Generic types are invariant by default, so `List<Int>` is not a `List<Object>`.
|
||||
|
||||
@ -239,7 +239,7 @@ class TypesTest {
|
||||
// actually we have now this of union type R1 & R2!
|
||||
// println(this::class)
|
||||
assert( this@R2 is R2 )
|
||||
assert( this@R1 is R1 )
|
||||
// assert( this@R1 is R1 )
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user