Skip to content

with val syntax sugar for more complex types #707

@jiribenes

Description

@jiribenes

Problem statement

There's no way to unpack more complex types the using with val _ = ... syntax sugar.

Motivation

When working with stream, I tried to do something like:

def main() = {
  with val (i, x) = [(1, 'a'), (2, 'b'), (3, 'c')].foreach;
  println(show(i) ++ ": " ++ show(x))
}

in order to unpack the tuple.
However, that's wrong since this sugar would imply that foreach takes two arguments
as it desugars to something like:

...foreach { (i, x) => ... }

But the correct diet, sugar-free version is the following:

def main() =
  [(1, 'a'), (2, 'b'), (3, 'c')].foreach { case (i, x) => 
    println(show(i) ++ ": " ++ show(x))
  }

Possible and impossible workarounds

I tried to remedy this by using Tuple2(i, x) explicitly, but that reports a parse error:

def main() = {
  with val Tuple2(i, x) = [(1, 'a'), (2, 'b'), (3, 'c')].foreach;
  //             ^ Expected = but got (

  println(show(i) ++ ": " ++ show(x))
}

Similarly if I try to use a custom type:

record Pos(i: Int, x: Char)
def main() = {
  with val Pos(i, x) = [Pos(1, 'a'), Pos(2, 'b'), Pos(3, 'c')].foreach;
  //          ^ Expected = but got (

  println(show(i) ++ ": " ++ show(x))
}

What does work is the following:

def main() = {
  with val tup = [(1, 'a'), (2, 'b'), (3, 'c')].foreach;
  val (i, x) = tup;
  println(show(i) ++ ": " ++ show(x))
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions