Skip to content

Instantly share code, notes, and snippets.

@slyphon
Last active December 30, 2015 23:59
Show Gist options
  • Save slyphon/7904292 to your computer and use it in GitHub Desktop.
Save slyphon/7904292 to your computer and use it in GitHub Desktop.
I'm way too happy that I was able to get this to work (specifically OMGThatsUseful.soAmaze)
sealed abstract class BaseClass {
def name: String
def someData: Option[Int]
}
trait NamedThing extends BaseClass {
def name: String
}
trait WhatDoTheTypesSay[This <: NamedThing] {
def setSomeData(i: Int): This
}
case class Herp(name: String = "this one", someData: Option[Int] = None)
extends BaseClass
with NamedThing
with WhatDoTheTypesSay[Herp] {
def setSomeData(i: Int) = copy(someData = Some(i))
}
case class Derp(name: String = "that one", someData: Option[Int] = None)
extends BaseClass
with NamedThing
with WhatDoTheTypesSay[Derp] {
def setSomeData(i: Int) = copy(someData = Some(i))
}
object OMGThatsUseful {
def soAmaze[T <: NamedThing](bc: WhatDoTheTypesSay[T]): BaseClass = {
bc.setSomeData(42)
}
def callThat(): Seq[BaseClass] = {
val herp = Herp()
val derp = Derp()
Seq(soAmaze(herp), soAmaze(derp))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment