Created
January 7, 2012 03:15
-
-
Save diegopacheco/1573653 to your computer and use it in GitHub Desktop.
Scala Implicits Conversions DSL
This file contains hidden or 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
| import java.util.Date | |
| import java.util.Calendar | |
| object ImplicitsConversions extends App { | |
| class DateHelper(n:Int){ | |
| def days(when:String):Date = { | |
| var date =Calendar.getInstance() | |
| when match { | |
| case "ago" => date.add(Calendar.DAY_OF_MONTH,-n) | |
| case "from_now" => date.add(Calendar.DAY_OF_MONTH,n) | |
| case _ => date | |
| } | |
| date.getTime() | |
| } | |
| } | |
| object DateHelper{ | |
| val ago = "ago" | |
| val from_now = "from_now" | |
| implicit def convertInt2DateHelper(n:Int) = new DateHelper(n) | |
| } | |
| import DateHelper._ | |
| val past = 2 days ago | |
| val meeting = 3 days from_now | |
| println(past) | |
| println(meeting) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment