doc/manual: put back the manual in the main repository
[nit.git] / doc / manual / basic_type.md
1 # Basic Types
2
3 ## Object
4
5 Nit is a full object language. Every value is an instance of a
6 class. Even the basic types described in this section.
7
8 `Object` is the root of the class hierarchy. All other classes,
9 including the basic ones, are a specialization of `Object`.
10
11 Classes, methods and operators presented in this section are defined in
12 the standard Nit library that is implicitly imported by every module.
13 Many other classes and methods are also defined in the standard library.
14 Please look at the specific standard library documentation for all
15 details.
16
17 ## Int and Float
18
19 `1`, `-1` are `Int` literals, and `1.0`, `-0.1` are `Float`
20 literals. Standard arithmetic operators are available with a common
21 precedence rules: `*`, `/`, and `%` (modulo) ; then `+` and `-`. Some
22 operators can be composed with the assignment (`=`).
23
24 ~~~
25 var i = 5
26 i += 2
27 print i # outputs 7
28 ~~~
29
30 Conversion from `Int` to `Float` and `Float` to `Int` must be done with
31 the `to_f` and `to_i` methods.
32
33 ## String
34
35 Literal strings are enclosed within quotes (`"`).
36 To insert a value
37 inside a literal string, include the values inside braces (`{}`).
38 Braces has to be escaped.
39 `+` is the concatenation operator but is less efficient than the brace form.
40
41 ~~~
42 var j = 5
43 print "j={j}; j+1={j+1}" # outputs "j=5; j+1=6"
44 ~~~
45
46 Common escaping sequences are available (`\"`, `\n`, `\t`, etc.) plus the escaped brace `\{`.
47
48 ~~~
49 print "hel\"lo\nwo\{rld"
50 # outputs `hel"lo` on a first line
51 # and `wo{rld` on a second line
52 ~~~
53
54 Multi-line strings are enclosed with triple quotes (`"""`).
55 Values are inserted with a triple braces (`{{{value}}}`).
56 The multi-line form thus allows verbatim new-lines, quotes and braces
57
58 ~~~
59 print """some text
60 with line breaks
61 and characters like " and {
62 but {{{ 1+2 }}} is rendered as 3
63 """
64 ~~~
65
66 All objects have a `to_s` method that converts the object to a String.
67 `print` is a top-level method that takes any number of arguments and
68 prints to the standard output. `print` always add a newline, another
69 top-level method, `printn`, does not add the newline.
70
71 ~~~
72 var x: String
73 x = 5.to_s # -> the String "5"
74 print x # outputs "5"
75 ~~~
76
77 ## Bool
78
79 `true` and `false` are the only two `Bool` values. Standard
80 Boolean operators are available with the standard precedence rule:
81 `not`; then `and`; then `or`.
82
83 Common comparison operators are available: `==` and `!=` on all objects;
84 `<`, `>`, `<=`, `>=` and `<=>` on `Comparable` objects (which include
85 `Int`, `String` and others).
86
87 - `==`, `<`, `>`, `<=`, `>=` and `<=>` are standard Nit operators (so they are redefinable).
88
89 - `and`, `or` and `not` are not standard Nit operators: they are not
90   redefinable, also they are lazy and have adaptive typing flow
91   effects.
92
93 - `==` is not for reference equality but for value equality (like
94   `equals` in Java). There is a special reference equality operator,
95   `is`, but it cannot be redefined and its usage is not recommended.
96   Note also that while `==` is redefinable, it has a special adaptive
97   typing flow effect when used with `null`.
98
99 - `!=` is not a standard Nit operator. In fact `x != y` is
100   syntactically equivalent to `not x == y`.
101
102 ## Array
103
104 `Array` is a generic class, thus `Array[Int]` denotes an array
105 of integers and `Array[Array[Bool]]` denotes an array of array of
106 Booleans. Literal arrays can be declared with the bracket notation
107 (`[]`). Empty arrays can also be instantiated with the `new` keyword and
108 elements added with the `add` method. Elements can be retrieved or
109 stored with the bracket operator.
110
111 ~~~
112 var a = [1, 2, 3, 4] # A literal array of integers
113 print a.join(":") # outputs "1:2:3:4"
114 var b = new Array[Int] # A new empty array of integers
115 b.add(10)
116 b.add_all(a)
117 b.add(20)
118 print b[0] # outputs "10"
119 print b.length # outputs "6"
120 b[1] = 30
121 print b.join(", ") # outputs "10, 30, 2, 3, 4, 20"
122 ~~~
123
124 Note that the type of literal arrays is deduced using the static type
125 combination rule.
126
127 ## Range
128
129 `Range` is also a generic class but accepts only `Discrete`
130 types (`Int` is discrete). There are two kinds of literal ranges, the
131 open one `[1..5[` that excludes the last element, and the closed one
132 `[1..5]` that includes it.
133
134 ~~~
135 print([1..5[.join(":")) # outputs "1:2:3:4"
136 print([1..5].join(":")) # outputs "1:2:3:4:5"
137 ~~~
138
139 Ranges are mainly used in `for` loops.
140
141 ## HashMap
142
143 `HashMap` is a generic class that associates keys with values.
144 There is no literal hashmap, therefore the `new` keyword is used to
145 create an empty `HashMap` and the bracket operators are used to store
146 and retrieve values.
147
148 ~~~
149 var h = new HashMap[String, Int]
150 # h associates strings to integers
151 h["six"] = 6
152 print h["six"] + 1 # outputs "7"
153 ~~~