Primitives
Quick overview: Primitives
These are the built in types that can be used to represent information and build more complex types.
Strings
Quick overview: Strings
Create a string using double quotes:
let s = "Hello World!";
Concatenate strings using the ++
operator:
let s = "Hello " ++ "World!";
More String functions can be found in standard libraries:
- Native:
module String
- BuckleScript:
module Js.String
let s = String.trim(" extra whitespace\n");
/* "extra whitespace" */
let s = String.concat("\n", ["line 1", "line 2"]);
/* "Line 1\nLine 2" */
let s = String.sub("Hello World!", 6, 5);
/* "World" */
Char
Create a char using single quotes:
let c = 'a';
Access the char at an index using string.[index]
:
let c = "Hello".[1];
/* 'e' */
Convert char to string:
let s = String.make(1, 'c');
/* "c" */
let charArray = [| 'H', 'e', 'l', 'l', 'o' |];
let s = String.init(5, i => charArray[i]);
/* "Hello" */
Integer
Quick overview: Numbers
Integers are whole numbers. Their bit-size depends on the platform.
let x = 23;
let x = -23;
Standard operations include +
, -
, *
, /
, mod
:
let x = 23 + 1 - 7 * 2 / 5;
let x = 13 mod 2;
Integer literals
Different radix literals can be created using prefixes 0x
, 0o
, 0b
:
let decimal = 11256099;
let hex = 0xABC123;
let octal = 0o52740443;
let binary = 0b101010111100000100100011;
Literals can be broken up using the _
character which will be ignored:
let trillion = 1_000_000_000_000;
Bitwise operations
Use infix functions: land
, lor
, lxor
, lnot
, lsl
, lsr
, asr
from
module Pervasives
let x = 0b1010 lor 0b1100;
/* 0b1110, 14 */
Many bit tricks can be found here: bithacks
let isPowerOfTwo = x => {
x !== 0 && x land (x - 1) === 0
};
Float
Quick overview: Numbers
Floats are 64-bit numbers that may have a decimal. They follow the IEEE 754 standard.
let x = 23.0;
let x = 23.;
let x = -23.0;
Standard operations include +.
, -.
, *.
, /.
, **
:
let x = 3.0 +. 1.0 -. 7.0 *. 2.0 /. 5.0;
/* Exponentiation */
let x = 2.0 ** 3.0;
Boolean
Quick overview: Boolean
Reason supports a normal set of boolean and logical operations:
- Boolean operations:
!
,&&
,||
- Comparison:
>
,<
,>=
,<=
- Reference equality:
===
,!==
- Structural equality:
==
,!=
let x = true;
let y = !true;
let z = x && y || false;