File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Scala and the fly likes Expressiveness using implicit conversions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Scala
Bookmark "Expressiveness using implicit conversions" Watch "Expressiveness using implicit conversions" New topic
Author

Expressiveness using implicit conversions

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I was considering this post about the expressiveness of the Ruby Date/Time functions, and was wondering if the same type of syntax could be duplicated in Scala. After about 20 minutes hacking around, here's what I came up with. I'm not sure I'm completely in love with the abstraction, but the method calls look the same.

Output:
2 weeks from now: Sat Jul 26 14:58:44 MST 2008
10 days from now: Tue Jul 22 14:58:44 MST 2008
5 minutes ago: Sat Jul 12 14:53:44 MST 2008
[ July 12, 2008: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

I fiddled with your code before going to bed. This was a ton of fun!

Maybe what I did will spark some more ideas:

2 am: Sun Jul 13 02:00:00 EDT 2008
3 pm yesterday: Sat Jul 12 15:00:00 EDT 2008
noon tomorrow: Mon Jul 14 12:00:00 EDT 2008
12:05 - Sun Jul 13 12:05:00 EDT 2008

I was surprised that the Calendar treats noon as 12 am and midnight as 12 pm.


A good workman is known by his tools.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I'll see your extensions and raise you some more sugary sweet syntax.

1 day ago: Sat Jul 12 02:16:38 MST 2008
Yesterday at 3:00: Sat Jul 12 03:00:00 MST 2008
30 days from now at midnight: Tue Aug 12 00:00:00 MST 2008
Tomorrow at 3 AM: Mon Jul 14 03:00:00 MST 2008
30 days from 8 hours from now: Tue Aug 12 10:16:38 MST 2008
30 days + 8 hours from now: Tue Aug 12 10:16:38 MST 2008
[ July 13, 2008: Message edited by: Garrett Rowe ]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Pretty slick, Garrett, and Marc. Thanks for showing that. And Garrett - greetings from a former Arizonan.

[Marc]: I was surprised that the Calendar treats noon as 12 am and midnight as 12 pm.

What makes you say that? The documentation for Calendar says otherwise:

"Although historically not precise, midnight also belongs to "am", and noon belongs to "pm", so on the same day, 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Expressiveness using implicit conversions
 
Similar Threads
Eliminating Parenthesis within custom internal DSL
favorite groovy idiom
comparison of PHP with Ruby
Encapsulation and Scala
Comparing Dates with HQL