From 3d1ee506852abbcf64abe7d5f429613bc253b474 Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Thu, 2 Apr 2020 13:28:48 +0200 Subject: [PATCH] Tuple destructuring description --- .../docs/language-basics/sets-lists-tuples.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/gitlab-pages/docs/language-basics/sets-lists-tuples.md b/gitlab-pages/docs/language-basics/sets-lists-tuples.md index 31963eb26..b34c7327a 100644 --- a/gitlab-pages/docs/language-basics/sets-lists-tuples.md +++ b/gitlab-pages/docs/language-basics/sets-lists-tuples.md @@ -59,6 +59,52 @@ let full_name : full_name = ("Alice", "Johnson"); + + +### Destructuring + +If we want to get the first and last name of the `full_name` type, we can use +destructuring. Destructuring a tuple allows you to give names to the elements +inside the tuple. + +```cameligo group=tuple +let (first_name, last_name) : full_name = full_name +``` + +This also works in functions: + +```cameligo group=tuple +let first_name ((first_name, _): full_name) = first_name +let alice = first_name full_name +``` + +Notice that we use the underscore to indicate that we ignore the last element +of the tuple. + + + + +### Destructuring + +If we want to get the first and last name of the `full_name` type, we can use +destructuring. Destructuring a tuple allows you to give names to the elements +inside the tuple. + +```reasonligo group=tuple +let (first_name, last_name) : full_name = full_name +``` + +This also works in functions: + +```reasonligo group=tuple +let first_name = ((first_name, _): full_name) => first_name +let alice = first_name(full_name) +``` + +Notice that we use the underscore to indicate that we ignore the last element +of the tuple. + + ### Accessing Components