-
Notifications
You must be signed in to change notification settings - Fork 17
Type Declarations
David Jeske edited this page May 17, 2017
·
6 revisions
All types in Irken are inferred, making Type declarations optional. However, there are many situations where type declarations can improve readability.
Here is an example of a function which takes two parameters of type int
, and returns a string
.
(define (my-function a b) : (int int -> string)
(format (int a) " : " (int b)))
If you wish to return multiple arguments you can use a variant. Here we use a polymorphic variant with the empty name:
(define (my-function x) : (int -> (: int int))
(: x (+x 1)))
(match (my-function 1) with (: a b) -> (printf (int a) " - " (int b)))
Parametric type parameters are preceded by a single quote ('
), as in 'a
.
(define (my-function x) : ('a -> 'a)
x)
Here are some other type examples:
(list 'a) ;; the parametric list type
(list int) ;; a list of int
(int -> #u) ;; a function taking one int and returning nothing (void)
{a=int b=string ...} ;; an open record, matches any record with an integer "a"
;; field and string "b" field
{a=int b=string} ;; a closed record, matches any record with exactly an a
;; and b field
(:vec int string) ;; a polymorphic variant with an int and string
color ;; a static variant type (must match a datatype name)
|a=int| ;; a polymorphic variant row matching (:a int)
|a=int b=(int string)| ;; a polymorphic variant row matching either (:a int)
;; or (:b int string)