Built-in types¶
Introduction¶
Pineapple has special support for the following types:
-
Boolean
-
Nil
(also known as null) -
Number
andInteger
-
String
-
List
(also known as array) -
Tuple
(also known as array) -
Table
(also known as Dictionary or HashMap)
Boolean¶
Boolean types in Pineapple can be declared using #true
and #false
.
1 2 | let isHappy = #true let isSad = #false |
Boolean type is necessary for using control statements such as if-else
or while
loop.
For example,
1 2 3 4 5 6 | let isMad = #true if isMad "Oh my goodness".show else "Thank goodness".show |
Nil¶
Nil type is useful when you are not sure what to assign for a variable.
1 | let car = #nil // This means that you dont have a car |
Note
By default, you cannot assign #nil
to any variable. Check out Variables for more information.
Number and Integer¶
1 2 | let x = 123 // Will be inferred as Integer let y = 123.4 // Will be inferred as Number |
Info
Integer is any number that do not contains decimal values. It is especially useful for counting things, for example numberOfFruits
should be Integer instead of Number.
String¶
In Pineapple, strings are enquoted using double quotes.
1 | let message = "Hello world" |
String interpolation¶
You can also interpolate expressions into string using $()
.
1 2 3 4 5 6 | let fruit = "Pineapple" let howMany = 5 + 9 let message = "I like to eat $(fruit) every $(howMany.toString) days" message.show // I like to eat Pineapple 5 days |
Note
Every interpolated expression must have type of String, if not the Pineapple compiler will complain about it.
For example,
1 2 | let x = 23.4 let message = "My number is $(x)" // Error, `x` should be String, but it is Number |
To prevent such problem, you have to use the .toString
function.
Warning
Interpolated expression cannot be raw string. For example,
1 | let y = "$("yo")" // Syntax error |
List¶
List are useful for storing more than one elements. To create a list in Pineapple, you need to use square brackets [
]
and comma ,
.
1 2 3 4 5 6 | // Create list of integers let xs = [1,2,3,4] // You can pass in any expression as element let x = 99 let numbers = [.pi, 3 + 3, 7.square, x] |
List in Pineapple are homogeneous, it means that all elements within a list must be the same type. For example,
1 | let x = [1, "2"] // Error, the second element should be Integer |
Warning
You cannot create an empty list by using []
. Instead you need to use the List
constructor.
1 2 3 | let xs = [] // error let ys = List{Integer} // No error |
Multiline List¶
Sometimes your list might contains too many elements to be fit in a single line. In such situation you will need to use multiline list. For example,
1 2 3 4 5 6 | let fruits = o "Apple" o "Banana" o "Pineapple" o "Durian" o "Hello" |
Note
The indentation is necessary. So, the following is invalid in Pineapple:
1 2 3 4 | let fruits = o "Apple" o "Banana" // Error |
Reminder
Since o
is used as list bullet in Pineapple, you cannot named a variable as o
.
Table¶
Table is also known as Dictionary or HashMap. It is useful when you need to store a list of key-value data.
For example, let say you want to store phone numbers:
1 2 3 4 5 6 7 8 | // note that the indentation is necessary let phoneNumbers = "john" = "0123456" "babe" = "3333222" "wong" = "3212344" "lee" = "9843056" phoneNumbers.("john").show // "0123456" |
Empty table¶
To create an empty table, use the Table
constructor.
1 2 | // key , value let phoneNumbers = Table{String, String} |