tokiwa-software / fuzion

The Fuzion Language Implementation
https://fuzion-lang.dev
GNU General Public License v3.0
46 stars 11 forks source link

tuple destructuring in lambda args #425

Open michaellilltokiwa opened 2 years ago

michaellilltokiwa commented 2 years ago

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)) ->

    )
fridis commented 1 year ago

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

fridis commented 2 months ago

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.