Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Scala: Self type annotations and structured types

DZone's Guide to

Scala: Self type annotations and structured types

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

A few days ago I tweeted that I didn’t really see the point in structured types in Scala

Not sure I understand where you would use structural types in #scala instead of defining a method on a trait http://bit.ly/jgiW7b

…but today my colleague Uday came up with a cool way of combining self type annotations with structured types inside a trait we defined.

We had some code duplicated across two classes which looked roughly like this:

class OnePageType {
  lazy val peopleNodes = root \\ "SomeNode" \ "SomeSubNode" \ "People" \ "Person"
  private def fullName(personName: Node): String = // code to build person's name
 
  lazy val people: String = peopleNodes.map(fullName).mkString(", ")
}
class AnotherPageType {
  lazy val peopleNodes = root \\ "OtherNode" \ "OtherSubNode" \ "People" \ "Person"
  private def fullName(personName: Node): String = // code to build person's name
 
  lazy val people: String = peopleNodes.map(fullName).mkString(", ")
}

The first line is different but the other two are identical because the data is stored in exactly the same format once we get down to that level.

Since We want to keep the XPathish queries as descriptive as possible so that we don’t accidentally end up pulling the wrong elements onto the page, making those a bit looser wasn’t an option in this case.

Instead we pulled out a trait like so:

trait People {
  self: {val peopleNodes: NodeSeq} =>
 
  private def fullName(personName: Node): String = // code to build person's name
 
  lazy val people: String = peopleNodes.map(fullName).mkString(", ")
}

Which we include in the classes like this:

class OnePageType extends People {}
class AnotherPageType extends People {}

What we’re done on line 2 of the People trait is to define a self annotation which says that we need a val of peopleNodes to be present on the classes in which the trait is mixed.

If a val of peopleNodes doesn’t exist then the class won’t compile!

In this case the structure type works quite well because we wouldn’t really want to pull out peopleNodes into a trait just to reference it as a self type annotation.

 

From http://www.markhneedham.com/blog/2011/06/27/scala-self-type-annotations-and-structured-types/

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}