Home / Fennel / Type system design
Schemas
;; Type predicates
string, integer, ... ;; 8 built-in types in Lua
{:x integer :y integer} ;; product, map
{integer true} ;; product, set
[string] ;; product, sequence, homogeneous
[string number] ;; product, sequence, heterogeneous, fixed length
[:a :b :c] ;; sum, enumeration, sequence
{:a true :b true :c true} ;; sum, enumeration, set
(fn even? [value] ...) ;; custom
_ ;; anything
(type point {:x number :y number}) ;; new type, macro
(assert (supertype? vertex module.node)) ;; interoperability check
;; Sub-type checking.
;; New keyword: &in
(number 1) ;; evaluates to 1
(number :hi) ;; type error: `hi' is not a number
;; Type checking at function boundaries.
;; Goal: Avoid identifier-type repetition, encourage reuse.
;; New keyword: &to
(point origin) ;; check type
{: name : education &in person} ;; check sub-type, map
[first second &in pair] ;; check sub-type, sequence
(fn origin? [point &to boolean]) ;; Automatic types, all
(fn scale [point (number factor) &to point]) ;; Automatic types, some
(fn success? [{: status &in response} &to boolean]) ;; Destructuring, maps
(fn origin? [{: x : y &in point} &to boolean]) ;; Destructuring, maps
(fn first? [head &in list &to boolean]) ;; Destructuring, sequences
;; Automatic typing encourages:
;;
;; "It is better to have 100 functions operate on one data structure than 10
;; functions on 10 data structures. --- Alan Perlis