Skip to content

Instantly share code, notes, and snippets.

@lyricallogical
Created January 31, 2013 09:29
Show Gist options
  • Save lyricallogical/4681634 to your computer and use it in GitHub Desktop.
Save lyricallogical/4681634 to your computer and use it in GitHub Desktop.
method-dependent-types なくても普通に返値型は変えられますよの話
def toDate[Result](magnet: DateMagnet[Result]): Result = magnet.convert
trait DateMagnet[Result] {
def convert(): Result
}
object DateMagnet {
implicit def fromInt(tuple:(Int, Int, Int)) = new DateMagnet[java.util.Calendar] {
def convert() = {
val (year, month, date) = tuple
val c = java.util.Calendar.getInstance
c.set(year, month - 1, date, 0, 0, 0)
c
}
}
implicit def fromString(s:String) = new DateMagnet[java.util.Date] {
def convert() = new java.text.SimpleDateFormat("yyyy/MM/dd").parse(s)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment