diff options
Diffstat (limited to 'doc/2_02_list_map_struct_alias.adoc')
-rw-r--r-- | doc/2_02_list_map_struct_alias.adoc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/doc/2_02_list_map_struct_alias.adoc b/doc/2_02_list_map_struct_alias.adoc new file mode 100644 index 00000000..f10c1ec8 --- /dev/null +++ b/doc/2_02_list_map_struct_alias.adoc @@ -0,0 +1,77 @@ +List, Map, Struct and alias +=========================== + +Next to the basic types, there are the normal things that we can expect from scripting languages. +As colm is static and strong typed, it is very convienient to use alias as well. + +An example is probably much clearer then 1000 loc. + +[source,chapel] +.poker.md +---- +alias Value_t map<int,str> +values:Value_t = new Value_t() + +values->insert(0, "Ace") +values->insert(1, "1") +values->insert(2, "2") +values->insert(3, "3") +values->insert(4, "4") +values->insert(5, "5") +values->insert(6, "6") +values->insert(7, "7") +values->insert(8, "8") +values->insert(9, "9") +values->insert(10, "Ten") +values->insert(11, "Jack") +values->insert(12, "Queen") +values->insert(13, "King") + +alias Suit_t map<int,str> +suit:Suit_t = new Suit_t() +suit->insert(1, "hearts") +suit->insert(2, "spades") +suit->insert(3, "diamonds") +suit->insert(4, "clubs") + +struct Card_t + s:int + v:int +end + +alias Hand_t list<Card_t> + +struct Person_t + name:str + age:int + hand:Hand_t +end + +john:Person_t + +john = new Person_t() +john->name = "john" +john->age = 18 +john->hand = new Hand_t() + +card:Card_t = new Card_t() +card->s = 2 +card->v = 13 +john->hand->push(card) + +print("ok ", john->name, " ", john->age, "\n") +for card:Card_t in john->hand { + print("\n\t", suit->find(card->s), " ", values->find(card->v), "\n") +} +---- + +When we run this we get: + +---- +ok john 18 + + spades King +---- + + +NOTE: this also illustrates how to iterate through a 'list' and access elements in a 'map'. |