Created
May 19, 2014 10:24
-
-
Save bverbeken/2909060637008a4a7f13 to your computer and use it in GitHub Desktop.
How can I avoid duplication here?
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
object MyImplicits { | |
implicit def fieldString_To_OptionString(duck: {def getValue(): String}): Option[String] = { | |
duck match { | |
case null => None | |
case x => Option(x.getValue()) | |
} | |
} | |
implicit def fieldDecimal_To_OptionOfBigDecimal(duck: {def getValue(): java.math.BigDecimal}): Option[BigDecimal] = { | |
duck match { | |
case null => None | |
case x => Option(x.getValue()) | |
} | |
} | |
} |
octonato
commented
May 19, 2014
Thanks!
Just one tiny problem with this: fieldDecimal_To_OptionOfBigDecimal converts to java.math.BigDecimal, not the scala BigDecimal..
But I get the idea, and you'll get beer next time we meet ;)
You want the Scala version? In that case, you can't generify it that way. Your output is not the same as your input. You need two type parameters.
ps: yes, beer!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment