let person = {
name: "Alice",
age: 30,
address: null
};
interface Person {
id: number;
name: string;
age: number;
}
function findPersonById(people: Person[], id: number): Person | undefined {
for (const person of people) {
if (person.id === id) {
return person;
}
}
return undefined;
}
Charles Antony Richard Hoare
I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
type option('value) =
| None
| Some('value);
function divide(a: number, b: number): number
type result('a, 'e) =
| Ok('a)
| Error('e);
let divide = (a: number, b: number): result(string, int);
let divide_exc = (a: number, b: number): number;
@type result :: {:ok, integer()} | {:error, string()}
@spec divide(integer(), integer()) :: result
def divide(a, b) do
try do
result = divide!(a, b)
{:ok, result}
rescue
e in RuntimeError -> {:error, e}
end
end
def divide!(a, b) do
# make the division
end
type animal = "cat" | "dog" | "cow";
type option(int) =
| None
| Some(int);
type dice_value = 1 | 2 | 3 | 4 | 5 | 6;
type player = "Alice" | "Bob"
type roll_of_dice = {
player: player,
value: dice_value
}
roll_of_dice = dice_value * player
type intList =
| Empty
| Node({
value: int,
next: intList,
});
type state = {
isLoading: bool,
error: error | null,
data: data | null
}