fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

tuple destructuring in lambda args

Open michaellilltokiwa opened this issue 3 years ago • 2 comments

When using tuples I find we often want to de-structure them in lambdas:

ex =>
  [(1,"one"),(2, "two")]
    .forAll ((a) ->
      (num, str) := a
    )

I think a shorter syntax like this could be handy:

ex =>
  [(1,"one"),(2, "two")]
    .forAll (((num,str)) ->

    )

michaellilltokiwa avatar Sep 12 '22 13:09 michaellilltokiwa

I have added a page on destructuring to the design pages. This is still incomplete, but a point to collect ideas.

fridis avatar May 15 '23 09:05 fridis

Using infix ||> helps a bit here:

ex =>
  [(1,"one"),(2, "two")]
    .for_each (a -> a ||> (num, str) -> say "$num $str")

but partial application does not work yet, this code

ex =>
  [(1,"one"),(2, "two")]
    .for_each ||>((num, str) -> say "$num $str")

results in an error

 > ./build//bin/fz destr.fz 

/home/fridi/fuzion/clean/fuzion.1/destr.fz:3:20: error 1: No type information can be inferred from a lambda expression
    .for_each ||>((n,s) -> say "$n $s")

A lambda expression can only be used if assigned to a field or argument of type 'Function'
with argument count of the lambda expression equal to the number of type parameters of the type.  The type of the
assigned field must be given explicitly.
To solve this, declare an explicit type for the target field, e.g., 'f (i32, i32) -> bool := x, y -> x > y'.

one error.

fridis avatar Jul 22 '24 11:07 fridis