Data Types and Variables

Dinfio Documentation





Data Types

Dinfio provides two data types, known as Primitive and Reference data types. Primitives are data types which store simple values, including numbers, strings, and booleans. And References are data types that store arrays and objects.

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 an object to help IDE builds code completion list and code hint.

Primitive Data Types: Number, String, and Boolean

1. 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 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 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

Reference Data Types: Array and Object

1. 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 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)

Variable Scope

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 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


← Code Structure Index Operators →


Documentation version: 1.0.12. Updated on: 14 November 2022.
Dinfio is designed and written by Muhammad Faruq Nuruddinsyah. Copyright © 2014-2024. All rights reserved.