5 Nit is a full object language. Every value is an instance of a
6 class. Even the basic types described in this section.
8 `Object` is the root of the class hierarchy. All other classes,
9 including the basic ones, are a specialization of `Object`.
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
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 (`=`).
30 Conversion from `Int` to `Float` and `Float` to `Int` must be done with
31 the `to_f` and `to_i` methods.
35 Literal strings are enclosed within quotes (`"`).
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.
43 print "j={j}; j+1={j+1}" # outputs "j=5; j+1=6"
46 Common escaping sequences are available (`\"`, `\n`, `\t`, etc.) plus the escaped brace `\{`.
49 print "hel\"lo\nwo\{rld"
50 # outputs `hel"lo` on a first line
51 # and `wo{rld` on a second line
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
61 and characters like " and {
62 but {{{ 1+2 }}} is rendered as 3
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.
73 x = 5.to_s # -> the String "5"
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`.
83 Common comparison operators are available: `==` and `!=` on all objects;
84 `<`, `>`, `<=`, `>=` and `<=>` on `Comparable` objects (which include
85 `Int`, `String` and others).
87 - `==`, `<`, `>`, `<=`, `>=` and `<=>` are standard Nit operators (so they are redefinable).
89 - `and`, `or` and `not` are not standard Nit operators: they are not
90 redefinable, also they are lazy and have adaptive typing flow
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`.
99 - `!=` is not a standard Nit operator. In fact `x != y` is
100 syntactically equivalent to `not x == y`.
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.
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
118 print b[0] # outputs "10"
119 print b.length # outputs "6"
121 print b.join(", ") # outputs "10, 30, 2, 3, 4, 20"
124 Note that the type of literal arrays is deduced using the static type
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.
135 print([1..5[.join(":")) # outputs "1:2:3:4"
136 print([1..5].join(":")) # outputs "1:2:3:4:5"
139 Ranges are mainly used in `for` loops.
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
149 var h = new HashMap[String, Int]
150 # h associates strings to integers
152 print h["six"] + 1 # outputs "7"