Intro
In programming, data types is an important concept.
To be able to operate on variables, it is important to know something about the type.
Because all variables doesn't have the same type, the operations that can be performed on them are different.
Kin's Data Types
Kin has 5 Datatypes:
- String
- Number
- Boolean
- Null
- Object
The Object Datatype The object data type can contain:
- An object
- An array
Ex:
# Numbers:
reka length = 16
reka weight = 7.5
# Strings:
reka color = "Yellow"
reka lastName = "Johnson"
# Booleans
reka x = nibyo
reka y = sibyo
# Object:
ntahinduka person = {firstName:"John", lastName:"Doe"}
# Array object:
ntahinduka cars = ["Saab", "Volvo", "BMW"]Note: By default an unannotated Kin variable can hold any type of data.
Kin Types are Dynamic (unless annotated)
Kin has dynamic types for unannotated bindings. The same name can hold different data types:
reka x; # Now x is ubusa. The semicolon is required when you omit the value.
x = 5 # Now x is a Number
x = "John" # Now x is a StringYou can opt in to fixed types with an annotation (: type or : type?).
Annotations are tokenized and parsed (not erased) and checked at runtime.
See Variables.
Type aliases (ubwoko)
Declare a named type with the ubwoko keyword. The same word is also a
prefix typeof operator: ubwoko(x) / ubwoko x yields a type value
(identity-comparable; prints as e.g. umubare). For class instances it returns
the class value itself — see Classes.
Object types, unions, and Fata (pick keys) are supported. Types may nest:
ubwoko Address = {
city: ijambo,
street: ijambo
}
ubwoko Person = {
name: ijambo,
age: umubare,
address: Address
}
ubwoko Id = ijambo | umubare
ubwoko PersonName = Fata<Person, "name">
ubwoko PersonNameAge = Fata<Person, "name" | "age">
reka keza: Person = {
name: "Keza",
age: 20,
address: { city: "Kigali", street: "KG 1" }
}
reka id: Id = "A-1"
reka onlyName: PersonName = { name: "Keza" }| Feature | Syntax | Meaning |
|---|---|---|
| Object type | { key: type, ... } | Structural shape of an object (ubwoko_imiterere) |
| Union | A | B | Value may be either type |
| Fata | Fata<T, "a" | "b"> | Object type with only the listed keys |
| Optional | type? | Type or ubusa |
| Nesting | address: Address | Property typed with another named type |
Mismatch example:
reka name: ijambo = 42
# Cannot assign a umubare to a ijambo, expected a ijambo instead.Kin Strings
A string (or a text string) is a series of characters like "John Doe".
Strings are written with quotes.
Ex:
# Sample string:
reka carName = "Volvo XC60"_Note: _ Only double quotes are supported in kin, so this will produce an error
reka x = 'this is not supported' # This will produce an errorKin Numbers
Numbers can be written with or without decimals.
So in Kin numbers can be either integers or floating point numbers.
Ex:
# With decimals:
reka x1 = 34.00
# Without decimals:
reka x2 = 34_Note: _ In Kin, there is no support for Exponential Notation.
Kin Booleans (nibyo, sibyo)
Booleans can only have two values: nibyo or sibyo.
Ex:
reka x = 5
reka y = 5
reka z = 6
(x == y) # Returns true
(x == z) # Returns falseBooleans are often used in conditional testing.
Kin Arrays
Kin arrays are written with square brackets.
Array items are separated by commas.
The following code declares (creates) an array called names, containing three items (people's names):
reka names = ["John", "Doe", "Jane"]Array indexes are zero-based, which means the first item is [0], second is [1], and so on.
So,
names[0] # Returns "John"
names[1] # Returns "Doe"
names[2] # Returns "Jane"Kin Objects
Kin objects are written with curly braces .
Object properties are written as name:value pairs, separated by commas.
Ex:
ntahinduka person = {firstName:"Bwenge", lastName:"Kana", age:15, eyeColor:"red"}The object (person) in the example above has 4 properties: firstName, lastName, age, and eyeColor.
We'll discuss more about objects later in these docs.
Kin Null (ubusa)
This is a special data type that can only have one value: ubusa.
It is used to represent the absence of a value.