Raz · lesson 4 of 15

Values and conversions

This program uses several of the basic types at once, with each conversion written where it happens.

Values.raz
module Values;
import Fmt;

const Max = 100;    (* untyped: it takes the type it meets *)

proc main*;
var n: INT; b: U8; big: I64; x: F64; c: I32; ok: BOOL;
begin
    n := Max * 3;
    b := 250;
    b := b + 10;          (* wraps, in a U8: 4 *)
    big := n;             (* an INT goes into an I64 *)
    x := F64(n) / 7.0;    (* nothing else converts unasked *)
    c := '\u{E9}';        (* a character is its code point *)
    ok := (n > Max) and (b # 0);
    Fmt.print("{} {} {} {} {} {}\n", [Fmt.int(n), Fmt.int(I64(b)),
        Fmt.int(big), Fmt.real(x, 3), Fmt.int(I64(c)),
        Fmt.bool(ok)]);
end;

end Values.
300 4 300 42.857 233 true

Conversions are written

Two types never mix but where nothing can be lost: an INT goes into an I64 wherever it meets one, so with pos: I64, pos + LEN(s) is an I64; and a U8, I8 or I16 into an INT, unless an operator made it, so n := s[i] needs no conversion. With i: I32 and a slice s, i < LEN(s) is an error. A type's name converts: F64(n), I64(b), U8(x); an I64 becomes an INT only so, INT(pos), since an INT may be smaller on another machine. The lesson on integers says what each width conversion does.

A number written in the program is untyped until it meets a type, and 1.5 is a real, which an integer never becomes without a conversion.

Mix.raz
module Mix;
import Out;

proc main*;
var i: I32; s: []const U8;
begin
    s := "four";
    i := 3;
    if i < LEN(s) then Out.str("smaller\n"); end;
end;

end Mix.
$ raz Mix.raz
Mix.raz:9:8: error: I32 and INT are different types
    9 |     if i < LEN(s) then Out.str("smaller\n"); end;
      |        ^^^^^^^^^^
help: convert one to the other's type, as in INT(i)
raz: 1 error

The fix is the conversion, written where it happens: if INT(i) < LEN(s). It costs nothing at run time and says what is meant.

What converts to what

Everything else that converts does so with the type's name, and the rules are short:

FromToResult
integerintegercut to the new width, or extended by the sign (signed) or zeros (unsigned)
integerF32, F64the nearest real
F32, F64integerrounded toward zero
F32F64the same value; the other way, the nearest F32
enumerationintegerits position; and back, if there is a name at that position
setintegerits bits; and back
INT, UINTpointerthe address; and back

There is no conversion between BOOL and an integer: say what you mean with if. A BOOL is true or false, and and, or and not are for BOOLs: they bind below the relations, so i < n and a[i] # 0 needs no parentheses, and and and or evaluate their right operand only when it decides the result. #, not <>, is "different".

Truth.raz
module Truth;
import Out;

proc positive(n: INT): BOOL;
begin
    return n > 0;
end;

proc main*;
var a: [3]INT; i: INT; found: BOOL;
begin
    a := [4, 0, 9];
    i := 0;
    while (i < LEN(a)) and (a[i] # 0) do INC(i); end;   (* never reads a[3] *)
    Out.int(i); Out.ln;
    found := positive(i) or positive(-1);
    if found and not positive(0) then Out.str("true and true\n"); end;
end;

end Truth.
1
true and true

Reals

F32 and F64 are the reals. A real constant is written with a point, 1.5 or 2.0E-3. Fmt.real(x, places) writes one with that many digits after the point, and Math has the functions.

Reals.raz
module Reals;
import Fmt, Math;

proc main*;
var n: INT; x: F64; y: F32;
begin
    n := 22;
    x := F64(n) / 7.0;
    y := F32(x);
    Fmt.print("22/7 = {}\n", [Fmt.real(x, 6)]);
    Fmt.print("as F32 = {}\n", [Fmt.real(F64(y), 6)]);
    Fmt.print("sqrt 2 = {}, pi = {}\n", [Fmt.real(Math.sqrt(2.0), 5), Fmt.real(Math.pi, 5)]);
    Fmt.print("2^10 = {}, log2 of that = {}\n", [Fmt.real(Math.pow(2.0, 10.0), 1), Fmt.real(Math.log2(1024.0), 1)]);
    Fmt.print("rounded: {} {}\n", [Fmt.real(Math.round(2.5), 1), Fmt.real(Math.round(-2.5), 1)]);
    Fmt.print("truncated: {} {}\n", [Fmt.int(I64(2.9)), Fmt.int(I64(-2.9))]);
end;

end Reals.
22/7 = 3.142857
as F32 = 3.142857
sqrt 2 = 1.41421, pi = 3.14159
2^10 = 1024.0, log2 of that = 10.0
rounded: 3.0 -3.0
truncated: 2 -2

A real converts to an integer by rounding toward zero. Every variable starts at zero, a real's 0.0 and a BOOL's false included, so there is no uninitialised value to be surprised by.

Save a program under its module’s name and run it with raz run Name.raz.