Dinfio Documentation
Dinfio provides two data types, known as Primitive
and Reference
data types. Primitive
is a data type which stores simple value, including number, string, and boolean. And
Reference
is a data type that stores array and object.
A number, string, boolean, array, and object can be stored in a variable. Dinfio is dynamically-typed language, so a variable can store any data type without providing type annotation.
However, you can still provide annotation to a variable to help IDE builds code completion list and code hint.
1. Number: A number can be written in decimal, hexadecimal, and binary base format. 22
is an example of decimal base, 0x6F1E0A
is an example of hexadecimal base, and 10111b
is an example of binary base:
a = 22 ' Decimal base
b = 14.2 ' Decimal base with floating point
c = -120 ' Decimal base with minus
d = 0x6E ' Hexadecimal base
e = 10111b ' Binary base
2. String: String is a sequence of characters surrounded by double quotation marks ("). "My name is Clara"
is an example of string.
name = "Clara"
writeln("Hello, my name is " & name) ' Output: Hello, my name is Clara
3. Boolean: Boolean is a data type that has only one of two possible values: true
and false
.
a = true
b = false
c = false
d = !a ' d is false
e = a && b ' e is false
f = a || c ' f is true
1. Array: Array is used to store multiple values in a single variable—instead of declaring separate variables for each value. You can create an array by calling function array()
or using square brackets notation []
:
a = array() ' Create an empty array
b = array(10) ' Create an array with 10 elements
c = [] ' Create an empty array
d = [10, 20, 30, 40] ' Create an array with elements: 10, 20, 30, and 40
You can access an array element or assign its value by referring to the index number (array_name[index]
):
a = [1, 1, 2, 3, 5, 8, 13, 21]
writeln(a[4]) ' Output: 5
a[4] = 7
writeln(a[4]) ' Output: 7
You can also store arrays in an array (nested array):
data = [
["Clara", 22, "Jakarta"],
["Aisha", 20, "Bandung"],
["Mitchel", 16, "Boston"]
]
writeln(data[1][0]) ' Output: Aisha
writeln(data[0][2]) ' Output: Jakarta
2. Object: Object is used to store collections of data and more complex entities. Object is also an instance of a class. Object usually has properties/attributes and methods/functions.
You can create an object by calling function object()
or class_name()
or using curly brackets notation {}
:
a = object() ' Create an empty object
b = {} ' Also create an empty object
window = gui_window("Hello") ' Create an object of class gui_window
data = { ' Create an object with attributes
id: 1234,
name: "Clara",
age: 22
}
To access an attribute or function of an object, you can use dot symbol .
(object_name.attribute_name
or object_name.function_name()
).
writeln(data.name)
data.name = "Aisha"
window.settitle("This is Window")
window.show()
You can also provide object type annotation to an object to help IDE builds code completion list and code hint:
var gui_window: window = gui_window("Hello")
var gui_button: button = gui_button("OK", window)
There are two scopes in declaring variable: globally and locally. A global variable can be accessed anywhere in the code. You can declare a global variable using keyword global
:
start
global a = 10
do_something()
writeln(a) ' a is now 20
writeln(b) ' b is 100
stop
function do_something()
a = 20
global b = 100
stop
And a local variable, that can only be accessed in function block. You can declare a local variable using keyword var
:
start
global a = 10
do_something()
do_something_again()
writeln(a) ' a is still 10
stop
function do_something()
var a
a = 20
stop
function do_something_again()
var a
a = 40
stop
Note: If you declare a local variable in the Main Statements
block, it acts as a global variable:
start
var a = 10
do_something()
writeln(a) ' a is now 20, because a is global variable
stop
function do_something()
a = 20
stop
A keyword const
has been introduced in Dinfio 3.2.0 to declare a constant. A constant is a variable that its value cannot be reassigned.
However, if a constant is either an array or an object, its elements/attributes can still be updated. Use keyword const
to declare a constant:
const a = 10
writeln(a) ' Output: 10
a = 20 ' Invalid assignment to constant 'a'
const b = [2, 4, 6]
b[0] = 3 ' You can change the value of its elements
b = [5, 7, 9] ' But you cannot reassign constant 'b'.
' This will throw an error "Invalid assignment to constant 'b'"
const c = {
x: 2,
y: 4
}
c.y = 6 ' You can change the value of its attributes
c = { ' But you cannot reassign constant 'c'.
a: 5, ' This will throw an error "Invalid assignment to constant 'c'"
b: 7
}
Note: Constants always act as global variables, so that they can be accessed anywhere in the code.
← Code Structure | Index | Operators → |
Documentation version: 1.0.14. Updated on: 22 August 2024.
Dinfio is designed and written by Muhammad Faruq Nuruddinsyah. Copyright © 2014-2025. All rights reserved.