Types¶
Kiwi supports the following builtin types. To build your own type, see Structs.
| Type | Description | Documentation |
|---|---|---|
boolean |
A true or false value. |
See below for an example. |
bytes |
A byte array. | See below for an example. |
date |
A date-time object. | See Dates. |
float |
A floating point number. | See below for an example. |
generator |
A lazy sequence produced by a generator function. | See Generators. |
hashmap |
A dictionary of key-value pairs. | See Hashmaps. |
integer |
A 64-bit integer. | See below for an example. |
lambda |
An anonymous function. | See lambdas. |
list |
A list of values. | See Lists. |
none |
A null value. | See below for an example. |
object |
An instance of a struct. |
See Structs and Abstract Structs. |
pointer |
A pointer to a managed reference. | See example below. |
string |
A sequence of characters. | See Strings. |
boolean¶
A true or false value.
bytes¶
A byte array.
ascii_chars = [33..126].to_bytes()
println ascii_chars.to_string()
/# prints:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
#/
date¶
Represents a date and time. See Dates.
float¶
A floating point number.
generator¶
A lazy sequence produced by a generator function — a function that contains one or more yield statements. Calling a generator function does not execute its body; instead it returns a generator object that produces values on demand when iterated with for.
fn countdown(n)
while n >= 0 do
yield n
n -= 1
end
end
for x in countdown(3) do
print "${x} "
end
# 3 2 1 0
# typeof returns "generator"
g = countdown(5)
println typeof(g) # generator
hashmap¶
A dictionary of key-value pairs. See Hashmaps.
# A simple hashmap with a single key `language`.
hashmap = { "language": "kiwi" }
println(hashmap) # prints: {"language": "kiwi"}
println(hashmap.language) # prints: kiwi
integer¶
A 64-bit integer.
lambda¶
An anonymous function. Lambdas can be assigned identifiers for code reuse. See lambdas.
# Define a reusable lambda called `puts` which accepts a parameter called `s` and prints its value.
puts = with (s) do
println(s)
end
puts("Hello, World!") # prints: Hello, World!
list¶
A list of values. See Lists.
none¶
A null value. A value that points to nothing.
# Define a hashmap, `c`, with null values.
c = { "a": null, "b": null }
println(c) # prints: {"a": null, "b": null}
# Deserializing a JSON string with "null" values into a hashmap.
d = deserialize('{"a": null, "b": null}')
println(d) # prints: {"a": null, "b": null}
# Set the hashmap key values of `a` and `b` to non-null values.
d.a = false
d.b = 'hello'
println(d) # prints: {"a": false, "b": "hello"}
# Set the hashmap key values of `a` and `b` to null values.
d.a = null
d.b = null
println(d) # prints: {"a": null, "b": null}
# Set `d` to a null value.
d = null
println(d) # prints: null
# Explicitly checking if `d` is null.
if d == null
println "d is null" # prints: d is null
end
# Set `d` to a non-null value.
d = 0
# Explicitly checking if `d` is non-null.
if d != null
println "d is not null" # prints: d is not null
end
# Type coercion of null to a Boolean.
if !null
println "hello world" # prints: hello world
end
object¶
An instance of a struct. See Structs and Abstract Structs.
# Define an empty struct called `MyStruct`
struct MyStruct end
# Create an instance of `MyStruct`
inst = MyStruct.new()
println(inst)
# prints: [Object(struct=MyStruct, identifier=inst)]
¶
# Define an empty struct called `MyStruct`
struct MyStruct end
# Create an instance of `MyStruct`
inst = MyStruct.new()
println(inst)
# prints: [Object(struct=MyStruct, identifier=inst)]
pointer¶
A pointer to a managed reference. For internal use only.
cap = 3
chan = __chan_create__(cap)
__chan_send__(chan, "hello")
println __chan_recv__(chan) # prints hello
__chan_close__(chan)
string¶
Represents text data. See Strings.