Data Types
Also known as: data types datatypes mix mixing int plus float implicit conversion coercion promote primitive integer int long short byte float double decimal money currency char character string boolean conversion cast overflow number
● Beginner4 minBefore this: VariablesSpec 2.1
Every value has a type: whole numbers are Int, text is String, true and false are Bool, and so on.
fn main() {
const age = 31
const height = 1.68
const price: Decimal = 499.99
const name = "Asha"
const initial = 'A'
const member = true
print("{name} ({initial}), {age}, {height} m, pays {price}, member: {member}")
}The program ran and printed:
kavach run overview.kav
Asha (A), 31, 1.68 m, pays 499.99, member: trueYou didn't write a single type: the compiler works each one out from the value.
Built-in types
| Type | Holds | Example |
|---|---|---|
Int | a whole number, 64 bits | 31, -7, 1_000_000 |
Int8 Int16 Int32 | a smaller whole number | -128 |
UInt8 … UInt64 | a whole number that is never negative | 200 |
Float | a number with a fraction, 64 bits | 1.68 |
Float32 | a 32-bit float, what GPUs compute fastest | 0.5 |
Decimal | an exact decimal of any size: Kavach's big decimal, for money | 499.99 |
BigInt | a whole number of any size | 2 ** 100 |
Bool | true or false | true |
Char | one character | 'A', 'क' |
String | text | "Asha" |
Bytes | raw bytes, as in files and on the network | "hi".bytes() |
Json | JSON whose shape is known only while running | json.parse(text) |
Types built from other types
[T] | a list of T |
[K: V] | a map from keys K to values V |
Set<T> | each T once |
(A, B) | a tuple: a few values together |
T? | a T, or none |
type Name { … } | a record of your own |
type Name = A | B | a union: one of several cases |
(name: T, …) | an anonymous record |
fn(A) -> B | a function |
interface Name | any type that keeps a promise |
No null: none instead
Kavach has no null that any value could secretly be. A value that may be missing has a ? in its type, as in String?, and is none when missing. The compiler then makes you handle that case, here with ??, which gives a fallback: see Optionals.
JSON does have null. Read as a Json value it is Null, and a key that isn't there reads as Null too. Decoded into your own type, it becomes none:
use std.json
type User {
name: String
nickname: String?
}
fn main() ! Error {
const text = "\{\"name\": \"Asha\", \"nickname\": null}"
const doc = try json.parse(text)
print(doc["nickname"].is_null())
print(doc["missing"].is_null())
const user = try json.decode<User>(text)
print(user.nickname ?? "no nickname")
print(user.nickname == none)
}The program ran and printed:
kavach run null.kav
truetrueno nicknametrue
Writing null (or nil, None, undefined) in Kavach code is an error that says what to write:
fn main() {
let nickname: String? = null
print(nickname ?? "no nickname")
}The compiler refused the program:
kavach run null.kav
error: Kavach has no `null`: a value that may be missing has an optional type (`String?`), and the missing value is `none` (spec 2.7)Where a Json value is expected, none becomes JSON's null; more on Json values.
Kavach never turns one number type into another behind your back. An Int plus a Float is an error until you say which you want: see Converting types.
Limits and sizes
| What | Limit or size | Source |
|---|---|---|
Int | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64 bits); going past it panics | 01-built-in-types.md |
Int8, Int16, Int32 | -128 to 127; -32,768 to 32,767; -2,147,483,648 to 2,147,483,647 | 01-built-in-types.md |
UInt8, UInt16, UInt32, UInt64 | 0 to 255; 0 to 65,535; 0 to 4,294,967,295; 0 to 18,446,744,073,709,551,615 | 01-built-in-types.md |
Float | 64 bits: about 15 to 17 significant digits, up to about 1.8e308 | 01-built-in-types.md |
Float32 | 32 bits: about 7 significant digits, up to about 3.4e38 | 01-built-in-types.md |
Decimal | no limit: up to about 38 digits in a fast 128-bit form, more in a bigger one; a division that doesn't end keeps 18 digits after the point | 01-built-in-types.md |
BigInt | no limit: as many digits as the number needs | 01-built-in-types.md |
Every limit in Kavach, on one page: Limits and Sizes.