Skip to content
Adam
Language

Expressions

Operators, how tightly they bind, and the conversions Adam allows.

Examples: res\examples\language\expressions holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.

expressions_ops.adam
module exe expressions_ops;

var
  GA: int32;
  GB: int32;
  GFlags: uint32;
  GWide: int64;
  GSmall: int8;

begin
  GA := 7;
  GB := 2;
  println("{} {} {}", GA / GB, GA div GB, GA mod GB);
  println("{}", GA > GB ? "more" : "less");
  GFlags := 0b0101 | 0b0010;
  println("{} {}", GFlags, GFlags & 1 = 1);
  println("{} {}", 5 xor 3, 1 shl 4);
  GA++;
  println("{}", GA);
  GWide := GA;
  GSmall := int8(GWide * 10 - 10);
  println("{}", GSmall);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
3.5 3 1
more
7 true
6 16
8
70

Assignment

Assignment is an expression: := and the compound forms +=, -=, *=, /=, %=, &=, |=, <<= and >>=, right associative. ++ and -- are expressions too. = is always equality, never assignment; == and != are errors whose hint names = and <>.

Arithmetic

/ is always real division and div integer division: with two int32s, 7 / 2 is 3.5 and 7 div 2 is 3; mod gives the remainder. An arithmetic or bitwise result has the type C gives it by its usual arithmetic conversions: int8 + int8 is int32, int16 * float32 is float32. Each operand must still reach that type with no loss, so an int32 meets a float32 only as float32(LN).

Logic and bits

and, or and not are logical; &, |, ~, xor, shl and shr are bitwise. & and | bind as * and + do, so A | B = C means (A | B) = C. C ? A : B takes a bool condition and gives A or B. A comparison does not chain: A < B < C is no expression.

Conversions

No conversion hides a loss. A value converts implicitly only when every value of its type fits the target: integer widening, float32 to float64, a constant that fits. Anything else needs a cast such as int8(LBig), and the compiler says so:

errorSM0200…/samples/diagnostics_narrowing.adam(33,13)
'int64' does not convert implicitly to 'int32'; write int32(...)

Strings

A string takes + (joining), the comparisons and in; any other operator on it is an error. LS in ["red", "green"] tests a string against a list, and a span "a".."m" holds what sorts between its ends; the same bracket test works for numbers, chars, choices values and pointers.

Objects

X is T tests an object's dynamic type, and X as T is the checked cast, which raises when the object is not a T.

↑ ↓ moveEnter openEsc close