"Unpacking" classes into method argument lists

I see enthusiasm on the proposed solution, however I think the problem could also be solved through the prism of structural types, which I would like to describe here.

The support of structural typing merits a debate on its own, but I think it is better to have it in mind when designing a solution for arguments unpacking. Indeed, I believe better support for structural typing could “naturally” solve the stated problem in addition to solving many other problems. However, if the current proposal was implemented, and assuming at some point in the future the support for structural typing would be improved, then the situation would be confusing because we would have two different unrelated ways to implement arguments unpacking.

That being said, the solution I see via structural typing would have a limitation compared to the current proposal: unpacking arguments would be supported only in argument lists containing exactly one record type (see the example below). But maybe that limitation could be relaxed, I am not sure.

Let’s pretend that Scala supports a short syntax for structurally typed records. So, instead of the following:

type Movie = Selectable { val name: String; val year: Int }

We can write:

type Movie = (name: String, year: Int)

And we can write record literals with the following syntax:

val theMeaningOfLife: Movie = (name = "The Meaning of Life", year = 1983)

Now, assuming there is a method foo that takes a movie as a parameter:

def foo(movie: (name: String, year: Int)): Unit = ()
// or, with the type alias defined above
def foo(movie: Movie): Unit = ()

We can call the method foo as follows:

foo(name = "The Meaning of Life", year = 1983)

Which, in effect, is equivalent to arguments unpacking.

The way this works is similar to the existing (though controversial) auto-tupling mechanism. The call expands to the following:

foo((name = "The Meaning of Life", year = 1983))

Which is a regular method call with a record literal passed as a parameter.

I know the topic of structurally typed records is a can of worms, but I wanted to mention it in this discussion because it would also bring a solution the arguments unpacking problem.

11 Likes