Primitives
Patina provides access to a variety of primitives, most of them mapping directly to BEAM
primitive types. A sample includes:
Scalar Types
atomLiteral constants with nameintSigned variable-size integers (up to 2^(2^31))floatFloating point values (64-bit)charUnicode scalar values like'a','α'and'∞'booleithertrueorfalsebitsBit String - a contiguous store of a number of bits not necessarily divisible by 8binBinary; a subtype ofbitswith length divisible by 8strUTF-8 encoded string; a subtype ofbinfnref()Stateless function reference, similar to Elixir’s&Foo.bar/1fn()Callable function, may be a stateless reference or a closurepid,portandrefnative BEAM types that have no literalsrecordThe BEAM Native Record type- The unit type
(), whose only possible value is an empty tuple:()
Despite the value of a unit type being a tuple, it is not considered a compound type because it does not contain multiple values.
Compound Types
- Lists like
[1, 2, 3] - Tuples like
(1, true) - Maps like
#{"foo" => 1, "bar" => 2}
Variables can always be type annotated. Note that Patina can also infer types from context.
pub fn main() {
// Variables can be type annotated.
let logical: bool = true;
let a_float: float = 1.0;
let an_atom: atom = a"hello";
// A type can also be inferred from context.
let inferred_type; // Type float is inferred from another line.
inferred_type = add_floats(12.0, 3.14);
// A mutable variable's value can be changed - rebound to a new value.
let mut mutable = 12; // Mutable `int`
mutable = 21;
// Error! The type of a variable can't be changed.
// mutable = true;
// Variables can be overwritten with shadowing.
let mutable = true;
/* Compound types - List, Tuple and Maps */
// List's signature consists of type T as [T].
let my_list: [int] = [1, 2, 3, 4, 5];
// Tuple is a collection of values of different types
// and is constructed using parentheses ().
let my_tuple = (5, true, -5.04);
// Map's signature consists of Key type K and Value type V as #{K => V}
let my_map: #{float => str} = #{3.14 => "pi", 0.0 => "zero"};
/* Function types */
let add_ref: fnref(float, float) -> float = add_floats;
// fnref is implicitly convertible to fn
let add_fn: fn(float, float) -> float = add_ref;
}
fn add_floats(a: float, b: float) -> float {
a + b
}
Runtime representation
-
bits,binandstrare all the same Erlang binary data type. While some values of these types are distinguishable (an Erlang binary that doesn’t contain valid UTF-8 data is definitely not astr), in general you can’t rely on runtime checks to differentiate abinfrom astrorbits. -
charvalues are represented by single integers -
boolvalues are represented as atomsa"true"anda"false" -
all of the other primitive types are directly represented in BEAM