SCL Types

From Developer Documents
Jump to navigation Jump to search

Basic types

SCL is statically typed language which means that types of the possible values of all variables are known already at compile time. The following types (or more exactly, type constructors) have builtin support:

  • Boolean
  • Byte, Short, Integer, Long
  • Float, Double
  • String
  • BooleanArray, ByteArray, ShortArray, IntegerArray, LongArray, FloatArray, DoubleArray
  • []
  • (), (,), (,,), ...
  • (->)
  • Maybe
  • Array

Other type constructors are either imported from the host language or defined in SCL modules. Except for the couple of special cases in the previous list, the names of all type constructors are capitalized.

Some type constructors are parametric (compare to generics in Java or templates in C++). For example, the list type constructor [] has one parameter: the type of the list elements. Thus [Integer] is the type of the integer lists and [String] is the type of string lists. [[Integer]] is the type of the lists of integer lists. Parameters are usually written after the parametric type constructor: for example Maybe String or Array Integer, but some of the builtin type constructors can be written in a special way in order to make the type expressions more readable:

[a] = [] a
(a,b) = (,) a b
(a,b,c) = (,,) a b c
...
a -> b = (->) a b

Particularly important type constructor is (->) for building function types. For example, the type of the function computing the length of a string is String -> Integer: the function takes a string as a parameter and returns an integer.

Types of the functions taking multiple parameters are written by composing function types. For example, the type of a function taking nth element of a string list is [String] -> Integer -> String. The function takes a string list and an integer as a parameter and returns a string. Function type operator -> is right associative thus the previous type is equivalent to [String] -> (Integer -> String). Thus the type expression can be read as well as a type of functions taking a string list and returning another function from integers and strings.

(a,b) is the type of pairs where the first component of the pair has type a and the second component has type b. Tuple types (a,b,c), (a,b,c,d) etc. are defined similarly. Maybe a is the type of optional values.

Type variables

Many functions can be defined so that they do not need to know the exact types they are operating with. Such unknown types can be filled in type expressions by type variables: for example the function we discussed earlier that took nth element of a string list does not need the information that list elements are strings in its implementation and so it can be given a more generic type [a] -> Integer -> a, i.e a function taking a list of a:s and an integer as a parameter and returning a single a.