Section 5: Declarations
Last updated
Was this helpful?
Last updated
Was this helpful?
Evaluated declarations are actually inline expressions, which can be created using the following grammar:
=
expr
Not only that, the value of the evaluated expressions will also be shown on STDOUT. However, evaluated declarations will only be evaluated if they are written in the entry file, except for .
For example, if we interpret the following Keli program,we shall see 120
on STDOUT.
Constant declarations are useful for defining common constants such as the value of pi, e, etc. They can be declared using the following grammar:
=
expr
For example,
Once a constant is declared, it can be accessible anywhere within the same module (or those which imported it).
Constants cannot be reassign new values, thus the following is erroneous:
There are two kinds of function in Keli, namely unifunc and polyfunc.
Unifunc can be created using the following grammar:
where:
For example,
In the code above, the first this
is the parameter for the function square
, where this
should be type of Int
. The body of this function is this.*(this)
, while the return type of this function is also Int
.
Return type annotation for functions are optional (since it can be inferred), so the square
function can be rewritten as :
Polyfunc can be created using the following grammar:
where
For examples:
Same as unifunc, the return type annotation of polyfunc declaration is also optional.
For example, the following Keli code is valid:
Moreover, functions with overlapping identifiers are also permitted:
The following code is invalid, although the parameters name are different:
Also, Keli does not supports multiple dispatch based on return types, so the following code is invalid despite different return types:
Generic functions are functions whose parameters type are generic. We can declare generic functions using the following grammar:
typeVarId is any valid identifiers, while typeConstraint is any valid constraint expressions.
For example, the identity function can be defined as such:
In the code above, T
is the type variable identifier, while Any
is the constraint on T
, Any
also means no constraint.
The type variable T
is inferred using some sort of Hindley-Milner type inference system.
For example,
Generic function by itself is not too useful unless incorporated with generic types such as generic objects or generic tagged union.
Function specialization is a phenomenon where multiple dispatch and generic functions are used in synergy. This feature allows a function to be specialized when specified, and generic when unspecified.
Although normal user will rarely utilize this feature, it is crucial for library author to write generic modules.
A common example is the toString
function. It is a generic function, however, it may be specialized, as such:
Documentation for functions can be created using string expressions instead of comments. They can optionally appear:
before the function declaration
after each parameter
after return type annotation
For example,
Object type alias (a.k.a struct types) can be created using the following grammar:
For example,
Object type alias can be used as type annotations, for example,
Moreover, it can also be used as object constructor, as follows:
Tagged unions (a.k.a discriminated unions OR sum types) is consists of one or more tags connected together by the .or
function.
There are two kinds of tag, namely carryless tag and carryful tag.
Carryless tag are tags that does not carry any payload with them (like enums in C or Java). They can be created using the following grammar:
tagId
Example of carryless tags (note that the following piece of code is invalid, it's just for demonstration purpose):
Carryful tag are tags that carry some specific payload. They can be created using the following grammar:
Example of carryful tags:
Tag by themselves are not useful unless they are associated with a union. A union can be created using the following grammar:
unionId should follow the PascalCase
convention. tagDecl is either a carryless tag or a carryful tag.
For example,
unionId can be used as tag constructor prefix or type annotation.
For example, we can use the identifier Shape
to create carryless tag and carryful tag.
The type of x
and y
are both Shape
.
For example, we can use the identifier Color
as function parameter type annotation.
Type constructors (a.k.a generic types) are actually function that takes one or more types and return a new type. In Keli, there are 2 kinds of type constructor, namely object type constructor and tagged union type constructor.
Object type constructor can be declared using the following grammar:
For example, we can encode the tuple type as object type constructor as follows:
In the code above, Tuple
is the type constructor identifier, and it serves two purpose:
For constructing new Tuple
To be used as type annotation.
To construct a new Tuple
:
myTuple
will be inferred to have the type Tuple.fst(String) snd(Int)
.
To used Tuple
as type annotation:
Tagged union type constructor (a.k.a generic tagged union can be constructed using the following grammar:
For example, singly linked list can be defined as such:
The identifier List
can be used as:
Tag constructor prefix
Type annotation
Using List
as tag constructor prefix:
The type of x
is List.of(A)
where the type of y
is List.of(Int)
. Due to the type inference, the following expression is invalid:
Using List
as type annotation:
Interface are a kind of type constraint that allow user to define a set of functions on a specific type.
To define an interface in Keli, there are 3 steps required:
Define the name of the interface.
Define a set of functions required by the interface.
Define a set of default functions for the interface.
Interface can be defined using the following grammar:
Note that interface identifier should follow PascalCase
convention.
For example,
Required function can be defined using the following grammar:
{
{
typeVarId interfaceId}
funcSignature=
required
**}
Alert
To defined required function, the return type annotation cannot be omitted like usual functions do.
For example, the code below is saying that if a data type is Comparable
, then it must have the ==
function and >
function defined.
For example,
No special construct is needed to implement an interface, we just need to declare the required function by the interface for the specified data type.
unifuncSignature =
unifuncSignature = (
** )
**.
[ |
]
polyfuncSignature =
polyfuncSignature =(
**)
.
{ **(
**)
} [|
] =
Keli supports (a.k.a function overloading), thus it is possible to declare multiple function that have the same set of identifiers as long as their parameters type does not fully match.
{ {
}
} ( |
)
=
tagId are that follows the the PascalCase
naming convention.
tagId (
)
=
choice
{ .
}
.
{ (
)
**} =
n
.
{ (
)
**} =
choice
{** .
)
}
=
**interface
where funcSignature is either a or _[_polyfuncSignature](section-5-declarations.md#5-2-2-polyfunc-declarations).
Unlike object-oriented languages, where interface identifiers can be used as type annotation, interface identifier can only be used as type constraint . Thus, they can only appear in or .