Generic Types
Generic types are very common in Kitten. They describe the types of words that can be used generically, on many different types of data. For example, the concat
word has the following type:
<T> (List<List<T>> -> List<T>)
This means that for any type T
, the input of concat
is a nested list of lists of values of type T
, and its output is a flattened list of those values. For example:
>>> [[1, 2], [3, 4]] concat
[1, 2, 3, 4]
Generic types also describe data types that can be used generically to store different types of values. List
is the first example we’ve seen—List
by itself isn’t a type, it’s a type constructor: if you give it a particular type argument in angle brackets (<>
), then it becomes a concrete type like List<Char>
or List<List<Int32>>
.
There are several generic types in the standard library in addition to List
. The most commonly used one is Optional
, which is like a list that can only contain 0 values or 1 value. Optional values are often used as the outputs of functions that may fail to return a result, such as getting the value at a particular index in a list with get
; less commonly, they are also used as the inputs of functions with “default” values for their inputs. An “empty” Optional
is called none
, and a “full” Optional is called some
:
>>> //type none
<T> (-> Optional<T>)
>>> //type 5 some
-> Optional<Int32>
TODO:
Pair<A, B>
(product types and structure layout)Either<A, B>
(sum types and union layout)map<A, B> (List<A>, (A -> B) -> List<B>)