Skip to content

Instantly share code, notes, and snippets.

@leedm777
Forked from jbrechtel/traits.scala
Created March 13, 2012 13:42
Show Gist options
  • Save leedm777/2028836 to your computer and use it in GitHub Desktop.
Save leedm777/2028836 to your computer and use it in GitHub Desktop.
Traits in Scala
trait Searchable {
def search(query: SearchQuery): Seq[SearchResults]
}
trait PaginationWithNominalType {
this: Searchable =>
def page(query: SearchQuery, pageSize: Int, pageNum: Int) = {
search(query).grouped(pageSize).toList(pageNum)
}
}
trait PaginationWithStructuralType {
this: { def search(query: SearchQuery): Seq[SearchResults] }
def page(query: SearchQuery, pageSize: Int, pageNum: Int) = {
search(query).grouped(pageSize).toList(pageNum)
}
}
trait PaginationWithAbstractMethod {
def search(query: SearchQuery): Seq[SearchResults]
def page(query: SearchQuery, pageSize: Int, pageNum: Int) = {
search(query).grouped(pageSize).toList(pageNum)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment