cromo The Cromo Guide
PDF IT

Appendix A: Keywords and operators

#Keywords

KeywordUse
breakleaves a loop
casea branch of a switch
catchhandles an exception
constan immutable variable; a method that does not modify its object; a const func
continuegoes to the next round of a loop
defaultthe branch of a switch for the values not listed
elsethe alternative of an if
enumdeclares an enum
externdeclares C functions, in a module with import unsafe
false, truethe values of bool
finallyruns at every exit of a try
forthe loop, in all its forms
functhe type of a function
ifa condition
importimports a module
inin a for over the elements
interfacedeclares an interface
movemoves a value; a parameter or a capture that takes ownership
publicmakes a declaration visible outside its module
refa parameter or a capture that modifies; the elements of a for that can be modified
retreturns from a function
structdeclares a struct
switchchooses among cases
throwthrows an exception
trythe block whose exceptions are handled
unsafeimport unsafe: the dialect of C functions and raw memory
voidno result

print, println, eprint, eprintln, format, panic and assert are built-in functions, not keywords.

#Built-in types

TypeMeaning
i8, i16, i32, i64signed integers
u8, u16, u32, u64unsigned integers
f32, f64floating-point numbers
booltrue or false
chara byte, a synonym of u8
stringtext in UTF-8
list<T>a list that grows
map<K, V>a hash table
T[n]an array of fixed size
func(A, B) Ra function
addran address of C, only with import unsafe

#Operators

From the strongest to the weakest:

OperatorsMeaning
. () []member, call, index
! -not, inversion of the bits, negation
* / %multiplication, division, remainder
+ -addition, subtraction; + also joins strings
<< >>shifts
&bitwise and
^bitwise exclusive or
|bitwise or
== != < <= > >=comparisons
&&logical and, short-circuit
||logical or, short-circuit
= += -= *= /= %= &= |= ^= <<= >>=assignments, statements on their own
  • i++ and i-- are statements on their own, never parts of an expression.
  • There is no conditional operator ?:: write an if.
  • A shift beyond the width of the type is a panic in debug builds.
  • Assignments are statements: if (a = b) is not Cromo.

#Literals

LiteralExample
integer42, 1_000_000, 0xFF, 0b1010
floating point3.14, 1e-9, 2.5e3
character'a', '\n', '\t', '\\', '\'', '\x41'
string"text", with the same escapes and \"
list[1, 2, 3]

#Attributes

Attributes are written as in C++11, before what they apply to:

AttributeMeaning
[[test]]a test function
[[json("name")]]the name of a field or a case in JSON
[[json(skip)]]a field left out of JSON