= Raku Types = `raku(1)` has three essential data types, and they each feature a runtime type system. <> ---- == Scalars == A scalar is a single value. Scalars are named and referenced with a leading dollar sign (`$`). {{{ my $a = 1; }}} Scalars can be any of strings (`Str`), integers (`Int`), or rational numbers (`Rat`). Scalars can declared null, in which case they are typed as `Any`. If they are declared with any value, or if they are annotated with any type, they have the corresponding type. {{{ my Int $a; say $a.WHAT; # (Int) my $b; say $b.WHAT; # (Any) $b= 1; say $b.WHAT; # (Int) $b= "1"; say $b.WHAT; # (Str) $b= Nil; say $b.WHAT; # (Any) }}} If a scalar is annotated with a type and a mismatched value is assigned to it, a runtime error occurs. ---- == Arrays == An array is a list of values. Arrays are named and referenced with a leading at sign (`@`). Array elements are indexed with integers starting at 0 surrounded by square brackets (`[`,`]`). {{{ my @set = 'a','b','c'; @set[0] = 'd'; say @set; # [d b c] }}} With string arrays, there are multiple options for declarations. {{{ my @set = 'UK', 'Germany', 'France'; my @set = ; # tokenized by whitespace my @set = <<'United Kingdom' Germany France>>; # tokenized while respecting quotes }}} Arrays can be declared with a fixed size. {{{ my @set[3]; }}} If an out-of-range index is assigned to, a runtime error occurs. Arrays can also be declared as multi-dimensional. {{{ my @table[3;2]; }}} Arrays are typed according to the elements. {{{ my Int @set = 1,2,3; }}} ---- == Hashes == A hash is a mapping of keys to values. Hashes are named and referenced with a leading percent sign (`%`). Hashes are index by keys surrounded by angled brackets (`<`,`>`). These are equivalent declarations: {{{ my %map = '1','001','2','002'; my %map = 1 => '001', 2 => '002'; say %map<1>; # 001 }}} An empty hash can be declared in two ways: {{{ my %map = {}; my %map = %(); }}} Hashes are typed according to the values, not the keys. {{{ my Int %map = '1',1,'2',2; }}} To constrain the type of keys as well, try: {{{ my Int %map{Str} = '1',1,'2',2; }}} ---- == Enums == An enum is a mapping of names to values where the names themselves are the useful component. {{{ enum Names ( John => 0, Jane => 1 ); enum Names ( :John(0), :Jane(1) ); enum Names ; }}} Names are automatically stringified ''to their name'' as needed. {{{ my $a = Names::John; say $a; # John }}} Many core data types are actually implemented as enums. {{{ enum Bool ; enum Order (:Less(-1), :Same(0), :More(1)); enum Endian ; }}} ---- CategoryRicottone