Skip to main content

k# Type system

The propose model for k# is algebraic data types. All types should start with a capital letter. Int, String

Type aliases

It will possible in k# define aliases of types. The benefit of aliases is to encode meaning into the type.

type Age = Int

Tuples

Type product allows create type combinations. It is called tuples in other languages.

type Point = Int, Int

Unions type

Union types is used to define many combinations of types. they require a Type Label at the beginning. Each type label is separated using pipe |

type Maybe a = Just a | Nothing

Union types also are used to define enumerations

type Weekend = Saturday | Sunday

Function types

k# use currying function style like λ\lambda calculus. Each function receive an argument and return a value.

type Sum = Int -> Int -> Int

Function type declarations

Functions also can define their types using type declarations

add :: Num a -> Num a -> Num a
add a b = a + b

Those type declaration are optional, k# can infer them when compiling the functions

Parametric types

k# should support parametric types over all forms of types. e.g

type Map a b = Map a b
type Option a = Some a | None
type ToString a = a -> String

Parameters should stars with a lowercase letter.

Traits

k# support traits to define a type which support a collection of functions. A trait requires a type over the methods apply. This type is a parameter

trait Num a =
sum :: a -> a -> a
prod :: a -> a -> a

Traits in algebraic types

Use a trait is similar to using a type

type Sum a = Num a -> Num a -> Num a 
type NumMap k v= Map (Num k) v

Intersection types

They are used when you need a type who implements many traits

type OrdNum = Ord & Num

Type constraints

K# defines a shortcut to assign traits to a parameter

sum Num a => a :: a -> a -> a

It is the same as

sum a :: Num a -> Num a -> Num a

Using labels on types

k# allows labeled types while composing new types. It allows define records

type User = 
name: String
password: String

-- Same as
type User = String String

behind the scene k# creates functions for the labels with the definition

type name = User -> String
type name = User -> String -> User
type password = User -> String
type password = User -> String -> User
Note

Labels have meaning on tuples and unions, for the other types they are informative.

Constrained types

They are like aliases but with a expression, creating a sub set of the type

type PositiveNum a = Num a => it >= 0
type Age = Int => it > 0 && it < 200

Instancing types

All types in k# can be instanced using a function with the name of the type. e.g

user1 = (User name="hjerez" password="A password")
user2 = (User "hjerez" "A password")

Built-in types

  1. List = List type
  2. Map = Map key value
  3. Tuple = Int, Int
  4. Numbers = Byte, Short, Int, UInt, Long, Double, Float
  5. String = List Char or String
  6. Character = Char
  7. Unit type = ()