Skip to content

Instantly share code, notes, and snippets.

@nkallen
Created February 16, 2010 20:41
Show Gist options
  • Save nkallen/305900 to your computer and use it in GitHub Desktop.
Save nkallen/305900 to your computer and use it in GitHub Desktop.
trait Query { def i: Int }
// A Query and a Query Decorator
case class SimpleQuery(val i: Int) extends Query
case class TimingOutQuery(timeout: Int)(query: Query) extends Query { def i = query.i }
// Factories
type QueryFactory = Int => Query
// A thing that needs to make Queries
class QueryEvaluator(Query: QueryFactory)(k: Int) {
def apply() = Query(1 + k).i
}
// A specific set of decorators:
val myQueryFactory = SimpleQuery andThen TimingOutQuery(1) // andThen StatsColletingQuery(...)
// Using it
val queryEvaluator = new QueryEvaluator(myQueryFactory)(1)
println(queryEvaluator())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment