Skip to content
CCPEDIAby Unity Nodes
App Development

Accessor Function

2 posts158 views1 likesLast activity Dec 2022

  1. kanika_kapoorOP

    tuple_test = script do
    let
    my_key_value = (“Key”, 1)
    my_coordinate = (1.0 : Decimal, 2.0 : Decimal, 3.0 : Decimal)

    assert (fst my_key_value == “Key”)
    assert (snd my_key_value == 1)
    assert (my_key_value._1 == “Key”)
    assert (my_key_value._2 == 1)

    assert (my_coordinate == (fst3 my_coordinate, snd3 my_coordinate, thd3 my_coordinate))

    Not getting what fst3, snd3…thd3 are ? Doc syas they are accessor fxns but not able to print their value using debug

  2. Gary_Verhaegen 1

    You can largely ignore them. Here is a snippet of code that will hopefully illustrate matters:

    module Main where
    
    import Daml.Script
    import DA.Tuple (fst3, snd3, thd3)
    
    setup : Script ()
    setup = script do
      let pair = (1, 2)
      let triplet = ("a", "b", "c")
    
      assert $ pair._1 == 1
      assert $ pair._2 == 2
      assert $ triplet._1 == "a"
      assert $ triplet._2 == "b"
      assert $ triplet._3 == "c"
    
      assert $ fst pair == 1
      assert $ snd pair == 2
      assert $ fst3 triplet == "a"
      assert $ snd3 triplet == "b"
      assert $ thd3 triplet == "c"
    

    The functions fst and snd work only on 2-element tuples and, respectively, get you the first and second element of that pair. The function fst3, snd3, and thd3 wirk only on tuples of 3 elements and get you, respectively, the first, second, and third element of that triplet.

    They are largely made redundant by the existence of the ._x notation, which works on tuple size 2 to 5 included. (For tuples or size larger than 5 you do not get any accessor so pattern matching is your only option. You really should not be using tuples of size larger than 5.)