I agree; that is certainly a value from this proposal.
Python has a very different context than Scala. They had the kwargs
dict for a long time. It is somewhat of a standard approach at this point. It doesn’t call any constructors. It is just a syntactic sugar for passing a dict. In Scala, the unpack
, however, will appear in a kind of void. Maybe if we had something similar to the kwargs-like dicts, we could work with that.
I know that it is similar to what some people here were proposing, but I think we could try to combine it by implementing these steps:
Named tuples
Named tuples could be a de facto type-safe substitute for dynamic dicts in Python. So let’s say we have a named tuple like this:
type Foo = (a: Int, b: String)
Unpacking case classes
To make it possible to use case classes like Config
in this solution, we can introduce a parameterized type alias special-cased by the compiler - Unpack
. Unpack
would take a T <: Product
and, when possible, return a named tuple with its elements.
case class Config(a: Int, b: String)
type Foo = Unpack[Config] // Foo = (a: Int, b: String)
That way, we don’t have to use the Selectable
that I’d argue is meant to serve a bit different purpose (cc @prolativ)
Spreading named tuples in parameter lists
After one has obtained the named tuple, he would be able to spread it using a spread operator (like ..
)
def foo(bar: Int, ..conf: Unpack[Config]) = ???
Spreading named tuples as values in the call site
To accommodate for easy transformation back to the Config object, we could spread the named tuple at the call site:
def foo(bar: Int, ..conf: Unpack[Config]) =
val conf = Config(..conf)
???
I think that this is an approach that solves the problem that Python solved with their PEP. It doesn’t call any constructors, and would serve just as a syntactic sugar to skip the unnecessary parentheses and sometimes to be able to pass the params of an unpacked Product
without specifying them one by one.