Skip to content

Instantly share code, notes, and snippets.

@george-hawkins
Created September 21, 2022 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save george-hawkins/a9db64f05e14ea7d191bc4cf85dd64f6 to your computer and use it in GitHub Desktop.
Save george-hawkins/a9db64f05e14ea7d191bc4cf85dd64f6 to your computer and use it in GitHub Desktop.
package com.example
import java.time.{Duration, ZonedDateTime}
trait Customer
trait Priority
trait OvenState
trait Cooker {
def cook(customer: Customer, deliveryTime: ZonedDateTime, priority: Priority): Boolean
}
object CookieNames extends Enumeration {
type CookieName = Value
val GingerSnaps, ChocolateChip = Value
}
import CookieNames.CookieName
abstract class CookieCooker(overState: OvenState, cookingTime: Duration, name: CookieName) extends Cooker
class DelegatingCookieCooker(overState: OvenState, cookingTime: Duration, name: CookieName)(cookFn: (Customer, ZonedDateTime, Priority) => Boolean) extends CookieCooker(overState, cookingTime, name) {
def cook(customer: Customer, deliveryTime: ZonedDateTime, priority: Priority): Boolean = cookFn(customer, deliveryTime, priority)
}
object DslExample extends App {
def addCooker(cooker: Cooker): Unit = ???
def addCooker(overState: OvenState, cookingTime: Duration, name: CookieName)(cookFn: (Customer, ZonedDateTime, Priority) => Boolean): Unit = addCooker(new DelegatingCookieCooker(overState, cookingTime, name)(cookFn))
def getOvenState(): OvenState = ???
val cookingTime = Duration.ofMinutes(10)
addCooker(new CookieCooker(getOvenState(), cookingTime, CookieNames.GingerSnaps) {
override def cook(customer: Customer, deliveryTime: ZonedDateTime, priority: Priority): Boolean = {
// Use `customer`, `deliveryTime` and `priority` to cook a cookie and return true if successful.
???
}
})
addCooker(getOvenState(), cookingTime, CookieNames.GingerSnaps) { (customer, deliveryTime, priority) =>
true
}
val addCookersX = addCooker(getOvenState(), cookingTime, CookieNames.GingerSnaps)(_)
addCookersX { (customer, deliveryTime, priority) =>
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment