Question

I am looking over the documentation for Scalatra and noticed an interesting snippet of code for syntax I haven't seen yet on : http://www.scalatra.org/2.2/guides/persistence/introduction.html

Specifically, it's this bit:

trait DatabaseSessionSupport { this: ScalatraBase =>
  import DatabaseSessionSupport._

everything here makes sense except for the this: ScalatraBase => segment. What significance does it have here? Is it specific for the import below or for the entire trait?

Was it helpful?

Solution

That is called a "self-type annotation" and it requires that any use of trait DatabaseSessionSupport in an instantiable class must be accompanied by ("mixed in with") a type consistent with ScalatraBase. I have not looked at this specific code, but it is most likely a use of the so-called "Cake Pattern."

You can find many treatments of this concept both in Stack Overflow, in various blogs and in a classic paper by Odersky et. al. titled "Scalable Component Abstractions."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top