= Rhai • is tightly integrated with rust • syntax, types, and semantics are heavily inspired by rust language documentation: https://rhai.rs/book/language/comments.html • print("hello world") or debug("hello world") • Comments: //… /*…*/ //!… ///… • constants via 'const CONSTANTNAME = VALUE;' • string interpolation in backtick syntax: `Item #${count + 1} = ${item}` • last expression in {…} is expression value of this block • variable names are scoped inside {…} • variable lookup is done from inner-most to outer-most scope • variables are per default shadowed, but can be configured to lead to compilation error == Operators + - * / % ** += -= *= /= %= **= << >> > >= < <= <<= >>= & | ^ &= |= ^= && || == != a ?? b → return b [if a equals ()] or else a X in COLLECTION, X !in COLLECTION == Data types • {int = i64, u8, u16, u32, u64, i8, i16, i32, i64} • {range over int, inclusive range over int} • {float = f64, f64, f32} • decimal • bool • char • string [immutable] • array • blob [as vector over u8] • map of string to any • timestamp • Fn • {dynamic value, shared value} a.k.a. any type optionally w.r.t. concurrency • () a.k.a. void == Value handling • "let FOO = 42;" for assignment. "let foo = bar();" for function calls • parse_decimal("42") for conversion • "dst = take(src);" *moves* data instead of copying them == Control statements if foo(x) { print("It's true!"); } else if bar == baz { print("It's true again!"); } else { print("It's finally false!"); } let a = [42, 123, 999, 0, true, "hello", "world!", 987.6543]; for (item, count) in a { if x.type_of() == "string" { continue; } print(`Item #${count + 1} = ${item}`); if x == 42 { break; } // break out of for loop } switch calc_secret_value(x) { 1 => print("It's one!"), 2 => { print("It's two!"); print("Again!"); } 3 => print("Go!"), 4 | 5 | 6 => print("Some small number!"), _ => print(`Oops! Something's wrong: ${x}`) } while x > 0 { x -= 1; if x < 6 { continue; } // skip to the next iteration print(x); if x == 5 { break; } // break out of while loop } do { … } while x > 0; do { … } until x == 0; loop { … } == Functions • functions in Rhai are pure and they form individual compilation units • function may be overloaded by arity • functions can only be defined at the global level, never inside a block or another function • functions are not closures and cannot access outside variables but may other functions and access modules • constants of module may be referred to with global::CONSTANTNAME fn add(x, y) { x + y } Special variable this in 'object.method(parameters …)' call syntax: fn change() { this = 42; } let x = 500; x.change(); // call 'change' in method-call style, 'this' binds to 'x' x == 42; // 'x' is changed! change(); // <- error: 'this' is unbound Elvis operator 'object?.method(parameters…)': do not call method if object is () function call with type checking on `this` variable: fn "type name string".method(parameters …) {…} e.g. fn int.do_update(x, y) { this += x * y } • An Fn type value may be call using `func.call(args…);` == Namespacing import "message" as msg; → msg::my_identifier_of_a_function();