Last active
December 30, 2015 23:59
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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