Last active
August 29, 2015 14:03
-
-
Save castaneai/d635e82171df09a61900 to your computer and use it in GitHub Desktop.
implicitを用いたパケットを扱うクラスを型で自動分岐する例 もっと簡潔に書けそうで思いつかない.
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
trait Packet | |
class ItemPacket extends Packet | |
class QuestPacket extends Packet | |
object PacketHandler { | |
trait PacketHandler[T <: Packet] { | |
def handle(packet: T) | |
} | |
implicit object ItemPacketHandler extends PacketHandler[ItemPacket] { | |
def handle(packet: ItemPacket) = println("item packet!") | |
} | |
def handle[T <: Packet](packet: T)(implicit handler: PacketHandler[T]) { | |
handler.handle(packet) | |
} | |
} | |
object Main { | |
def main(args: Array[String]) { | |
PacketHandler.handle(new ItemPacket) // => "item packet!" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment