Keli Language
  • Motivation
  • Showcase
  • Design goals
  • Features
  • Specification
    • Section 1: Introduction
    • Section 2: Lexical Structure
    • Section 3: Basic Expressions
    • Section 4: Magic Expressions
    • Section 5: Declarations
    • Section 6: Modules
    • Section 7: Type annotations
    • Section 8: Packages
  • Style guide
    • Naming conventions
  • Techniques
    • Singleton
    • Default values for functions
  • Installation
    • How to install Keli?
  • Tutorial
    • Creating a new package
  • CONTRIBUTE
    • Github source
  • Blog
    • Interval comparison operator for free!
Powered by GitBook
On this page
  • 7.0 Purpose of type annotations
  • 7.1 Built-in types
  • 7.1.1 Integer
  • 7.1.2 Float
  • 7.1.3 String
  • 7.1.4 Object
  • 7.1.5 Array
  • 7.1.6 Lambda
  • 7.2 Type constraint annotation
  • 7.2.1 No constraint
  • 7.2.2 Interface constraint
  • 7.2.3 Row type constraint

Was this helpful?

  1. Specification

Section 7: Type annotations

PreviousSection 6: ModulesNextSection 8: Packages

Last updated 6 years ago

Was this helpful?

7.0 Purpose of type annotations

The primary purpose of type annotations are for type checking, so that we can prevent a handful of bugs to be introduced into our program albeit it cannot detect logical errors. Moreover, type annotations can also serve as useful documentations.

Since Keli is a minimal language, type annotations are actually basic expressions in disguise. For example, the type annotation List.of(Int) is actually a polyfunc invocation.

In Keli, there are 6 places where we can write down type annotations:

  1. Function parameters

  2. Function return type

  3. Object property value type

  4. Carryful tag carry type

  5. Lambda parameter

  6. Type casting

Although a type annotation can be consist of any valid identifiers, the conventions in Keli dictates all type annotations to be written in PascalCase except for special circumstances.

7.1 Built-in types

Built-in types are types that can't or isn't encoded in Keli. For example, integers can actually be encoded as using tagged unions, but due to performance issues we chose to support integer type directly.

7.1.1 Integer

Annotation

Sample value

Int

123

7.1.2 Float

Annotation

Sample value

Float

3.142

7.1.3 String

Annotation

Sample value

String

"Hello world"

7.1.4 Object

Object type annotation can be created using the following grammar:

$ . { key ( typeAnnotation ) }

For example,

$.name(String) age(Int)

A valid value for the type annotation above is:

object.name("Keli") age(99)

7.1.5 Array

Array type annotation can be created using the following grammar:

Array . of ( typeAnnotation )

For example,

Array.of(Int)

A valid value of the type annotation above is:

[1,2,3,4,5]

7.1.6 Lambda

The type annotation for lambda expression (or functions) can be created using the following grammar:

Function . in ( typeAnnotation ) out ( typeAnnotation )

For example,

Annotation

Sample value

Function.in(Int) out(Int)

`(x

x.square)`

Function.in(Int) out(Function.in(Int) out(Int))

`(x

y

x.+(y))`

7.2 Type constraint annotation

7.2.1 No constraint

To specify no constraint, we can use the Any identifier. For example,

{T Any}
(this T).identity | T = this

7.2.2 Interface constraint

7.2.3 Row type constraint

For example, we can declare a generic function that takes any objects with the property age .

{T Type.extends($.age(Int))}
(this T).isOld | Boolean = this.age.>=(50)

Then, we can pass in object of any shape, as long as it has the property age.

= $.name("Keli") age(50).isOld // No error
= $.age(20).isOld // No error
= $.job("Programmer") age(40) // No error
= $.name("Pine").isOld // Error, missing property `age(Int)`

Type constraint annotation are used in and . Its purpose is to limit the kind of types that can be passed into specific generic functions and generic types. In Keli, there are 3 kinds of type constraint, namely no constraint, interface constraint or row type constraint.

Refer .

Row type constraint are used for achieving . Syntactically, a row type constraint annotation is no different than a , they only differs on the place where they are written.

Peano numbers
row polymorphism
object type annotation
generic functions
generic types
Section 5