Type casting - Palamecia/mint GitHub Wiki
Some operators only work with data of the same type:
- Operators applied to some built-in types
- The
:=
operator
When data of different types are passed to those operators, an automatic cast is applied to the right part of the operation. This automatic cast mechanism is only available for built-in types. On user-defined types, the cast must be done explicitly.
Note
The module mint.type also provides functions to explicitly cast built-in types.
The following table shows the available cast mechanisms:
from / to | none | null | boolean | number | string | regex | array | hash | iterator | package | function | library | libobject |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
none | โ | โ | โ | โ | โ | โ | |||||||
null | โ | โ | โ | โ | โ | โ | |||||||
boolean | โ | โ | โ | โ | โ | โ | |||||||
number | โ | โ | โ | โ | โ | โ | |||||||
string | โ | โ | โ | โ | โ | โ | |||||||
regex | โ | โ | โ | โ | โ | ||||||||
array | โ | โ | โ | โ | โ | ||||||||
hash | โ | โ | ? | โ | โ | ||||||||
iterator | โ | โ | โ | โ | โ | โ | |||||||
package | โ | โ | โ | โ | ? | โ | |||||||
function | โ | โ | โ | โ | ? | โ | |||||||
library | โ | โ | โ | โ | ? | โ | |||||||
libobject | โ | โ | โ | โ | ? | โ |
-
boolean: create a boolean initialized to
false
- string: create an empty string
- regex: create an empty regex
- array: create an empty array
- hash: create an empty hash
- iterator: create an empty iterator
-
boolean: create a boolean initialized to
false
-
string: create a string initialized to
'(null)'
-
regex: create a regex initialized to
/(null)/
-
array: create an array containing one
null
element -
hash: create a hash containing one
null
element mapped tonone
- iterator: create an iterator on the value
-
number: create a number initialized to
0
onfalse
or1
ontrue
-
string: create a string initialized to
'false'
onfalse
or'true'
ontrue
-
regex: create a regex initialized to
/false/
onfalse
or/true/
ontrue
- array: create an array containing the value
-
hash: create a hash containing the value mapped to
none
- iterator: create an iterator on the value
-
boolean: create a boolean initialized to
false
on0
ortrue
for other values - string: create a string initialized to the decimal representation of the number
- regex: create a regex matching to the decimal representation of the number
- array: create an array containing the value
-
hash: create a hash containing the value mapped to
none
- iterator: create an iterator on the value
-
boolean: create a boolean initialized to
true
- number: create a number initialized to the constant in the string; an error occurs if the string is not a number constant
- regex: create a regex matching the string's pattern
- array: create an array containing the value
-
hash: create a hash containing the value mapped to
none
- iterator: create an iterator on each character contained in the value
-
boolean: create a boolean initialized to
true
- string: create a string containing the pattern
- array: create an array containing the value
-
hash: create a hash containing the value mapped to
none
- iterator: create an iterator on the value
-
boolean: create a boolean initialized to
true
-
string: create a string initialized to the string representation of each element separated with
', '
between'['
and']'
- regex: create a regex matching the string representation's pattern (invalid object)
- hash: create a hash containing each index of the value mapped to the element
- iterator: create an iterator on each element of the value
-
boolean: create a boolean initialized to
true
-
string: create a string initialized to the string representation of each element separated with
', '
between'{'
and'}'
; keys and values are separated with' : '
- regex: create a regex matching the string representation's pattern
- array: create an array containing each key of the value
- iterator: create an iterator of iterators on pairs (key, value) of each element of the value
-
boolean: create a boolean initialized to
false
if the iterator is empty andtrue
otherwise - number: casts the next element to number
- string: casts the next element to string
- regex: casts the next element to regex
- array: create an array containing each next element
-
hash: create a hash containing each next element mapped to
none
-
boolean: create a boolean initialized to
true
-
string: create a string initialized to
'(package)'
-
regex: create a regex initialized to
/(package)/
- array: create an array containing the package
-
hash: create a hash containing the package mapped to
none
(invalid object) - iterator: create an iterator on the package
-
boolean: create a boolean initialized to
true
-
string: create a string initialized to
'(function)'
-
regex: create a regex initialized to
/(function)/
- array: create an array containing the function
-
hash: create a hash containing the function mapped to
none
(invalid object) - iterator: create an iterator on the function
-
boolean: create a boolean initialized to
true
-
string: create a string initialized to
'(library)'
-
regex: create a regex initialized to
/(library)/
- array: create an array containing the library
-
hash: create a hash containing the library mapped to
none
(invalid object) - iterator: create an iterator on the library
-
boolean: create a boolean initialized to
true
-
string: create a string initialized to
'(libobject)'
-
regex: create a regex initialized to
/(libobject)/
- array: create an array containing the libobject
-
hash: create a hash containing the libobject mapped to
none
(invalid object) - iterator: create an iterator on the libobject