Skip to content
Kavachकवच

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: true
overview.kav: Kavach code, then what running it prints

You didn't write a single type: the compiler works each one out from the value.

Built-in types

TypeHoldsExample
Inta whole number, 64 bits31, -7, 1_000_000
Int8 Int16 Int32a smaller whole number-128
UInt8 … UInt64a whole number that is never negative200
Floata number with a fraction, 64 bits1.68
Float32a 32-bit float, what GPUs compute fastest0.5
Decimalan exact decimal of any size: Kavach's big decimal, for money499.99
BigInta whole number of any size2 ** 100
Booltrue or falsetrue
Charone character'A', 'क'
Stringtext"Asha"
Bytesraw bytes, as in files and on the network"hi".bytes()
JsonJSON whose shape is known only while runningjson.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 | Ba union: one of several cases
(name: T, …)an anonymous record
fn(A) -> Ba function
interface Nameany 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
null.kav: Kavach code, then what running it prints

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)
null.kav: Kavach code, then what running it prints

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

WhatLimit or sizeSource
Int-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64 bits); going past it panics01-built-in-types.md
Int8, Int16, Int32-128 to 127; -32,768 to 32,767; -2,147,483,648 to 2,147,483,64701-built-in-types.md
UInt8, UInt16, UInt32, UInt640 to 255; 0 to 65,535; 0 to 4,294,967,295; 0 to 18,446,744,073,709,551,61501-built-in-types.md
Float64 bits: about 15 to 17 significant digits, up to about 1.8e30801-built-in-types.md
Float3232 bits: about 7 significant digits, up to about 3.4e3801-built-in-types.md
Decimalno 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 point01-built-in-types.md
BigIntno limit: as many digits as the number needs01-built-in-types.md

Every limit in Kavach, on one page: Limits and Sizes.

One page for each form

Check yourself

QUIZ 1 / 1pick one
Which type should hold a price like 499.99?