Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traits vs. Packages in Scala

After watching Martin's keynote on Reflection and Compilers I can't seem to get this crazy question out of my head. Martin talks among other things about the "(Wedding) Cake Pattern", where traits play the central part. I'm wondering, why in the world do we need packages when we already have traits? Is there anything a package can do, what a trait (at least theoretically) cannot?

I'm not talking about the current implementation, I'm just trying to imagine what programming would be like if we replace packages with traits. In my head it would be like this:

  • one keyword less (package is unneeded)
  • no need for package objects

To summarize all my questions:

  1. Is it theoretically possible to remove packages from the language and use traits instead.
  2. What other benefits would we gain from this change? (I was thinking about first class packages and first class imports, but mixin composition is a compile time thing, although the super calls are dynamically bound)
  3. Is Java/JVM compatibility the only thing, which would stand in the way?

Update

Daniel Spiewak talks in this keynote about the Dependency Injection being just the top of the iceberg of all the stuff you can do with the Cake Pattern.

like image 509
agilesteel Avatar asked Jul 28 '12 10:07

agilesteel


1 Answers

Martin Odersky has said that Scala could get by with just traits, objects, methods and paths (I hope I didn't forget something).

Both classes and packages are just there because Scala is intended to be a hosted language, i.e. a language which runs on (this is actually not the interesting bit) and interoperates with (this is the important point) a host platform. Some of the host platforms that Scala is intended to interoperate with are the Java platform and the CLI, both which have a concept of classes and packages (namespaces in the case of the CLI) that is significantly distinct enough that it cannot be easily expressed as traits or objects. This is unlike interfaces, which can be trivially mapped to and from purely abstract traits.

The above statement was made in a discussion about potentially removing generics from Scala, because everything generics can do can also be achieved by abstract types.

like image 98
Jörg W Mittag Avatar answered Sep 19 '22 16:09

Jörg W Mittag