Where does Integer get its year from in use(TimeCategory)?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Where does Integer get its year from in use(TimeCategory)?

Mike Schwager
Hi,
I have been studying a Groovy example:

use (org.codehaus.groovy.runtime.TimeCategory) {

def now = new Date()

def futureDate = now + 10.year

println "${now} => ${futureDate}"

}


...I don't understand where the Integer 10 gets the "year" method from.  It has something to do with TimeCategory, I know, because if I don't use() it, the code doesn't work. However, I am checking the Groovy API docs and in the TimeCategory class I can find no method or property "year".  (Note that 10.years will work above, as well, yet there is no such method or property in TimeCategory that I can find in the Groovy 1.6.0 docs.)


I need another hit with the cluestick.  Thanks.

-Mike Schwager


Reply | Threaded
Open this post in threaded view
|

Re: Where does Integer get its year from in use(TimeCategory)?

Jean Barmash-3
Mike,

I am also fairly new to Groovy, and don't  understand categories too well, but from http://docs.codehaus.org/display/GROOVY/Groovy+Categories

There are many situations where you might find that it would be useful if a class not under your control had additional methods that you define. In order to enable this capability, Groovy implements a feature borrowed from Objective-C , called Categories. There are a few categories that are included in the system for adding functionality to classes that make them more usable within the Groovy environment.

1.Year comes from this method:
  http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/TimeCategory.html#getYear(java.lang.Integer)

getYear

public static DatumDependentDuration getYear(Integer self)
Cheers,

Jean


From: Mike Schwager <[hidden email]>
To: [hidden email]
Sent: Friday, March 6, 2009 11:35:13 PM
Subject: [groovy-user] Where does Integer get its year from in use(TimeCategory)?

Hi,
I have been studying a Groovy example:

use (org.codehaus.groovy.runtime.TimeCategory) {

def now = new Date()

def futureDate = now + 10.year

println "${now} => ${futureDate}"

}


...I don't understand where the Integer 10 gets the "year" method from.  It has something to do with TimeCategory, I know, because if I don't use() it, the code doesn't work. However, I am checking the Groovy API docs and in the TimeCategory class I can find no method or property "year".  (Note that 10.years will work above, as well, yet there is no such method or property in TimeCategory that I can find in the Groovy 1.6.0 docs.)


I need another hit with the cluestick.  Thanks.

-Mike Schwager


Reply | Threaded
Open this post in threaded view
|

Re: Where does Integer get its year from in use(TimeCategory)?

Mike Schwager
Boy, I'm really having a hard time with some of these concepts.  On the face of it, everything is nice and there are lots of shortcuts that Groovy provides.  But when I really want to do something, I have an extremely hard time trying to understand how an author could have conjured up something.  I look at the api docs, and I see "getYear()" and "getYears()", but I don't see the attribute mentioned elsewhere in the doc- I need to just know that, "Hey Groovy programmer- there's a getYear() so there must be a year lurking in there!".  And there's no description of what TimeCategory does and how I might want to use it.  Throughout the documentation I have been reading, the authors seem to be these magicians conjuring up all kinds of neat stuff- "Ta-daa!  And look how easy that was!"  And it sure looks easy, until you start trying to wrap your mind around it.  Then, there's so much hocus-pocus taking place it twists the brain into a pretzel.

Honestly- and I'm sorry if this sounds inflammatory- but when I wrote a Ruby CGI script for work a couple of weeks ago, I had a big webpage/table thing up in 2 days, and it was pretty easy from start to finish.  Now I'm working with Groovy trying to scan some data and convert it to XML, and I'm spending hours just trying to understand how Groovy is pushing Java around, and what I can use, how it all works together, so on and so forth.  I had something working pretty easily but then I needed to change gears and get into Closures and oh boy did I stick my foot in it.

Anyway, here's what I said about your response.  I'm sure I'm wrong, but if it helps any of the Groovy gurus understand what some of us lesser mortals are going through, then maybe that's a good thing:
------------------------------------------------------------------------------------------

I don't think that's correct.  I've looked it over some more, and thought about it.  A couple of things need to be explained:

  • Where does Integer get a year method?  In the book "Programming Groovy" (Subramaniam) p. 203, the author says "Groovy categories provide a controlled way to inject methods".  So,
  • In order to inject a method, you must have created the method.  Where is that done in this example?  Sure, the TimeCategory class has a method getYear(), but 10 is an Integer
  • How is 10.year then added to an object of Class Date?
I think the explanation goes, in part, like this:

  • Groovy has operator overloading.  So when you are adding a Date + (what appears to be the Integer) 10, you are really doing:   TimeCategory.plus(Date, BaseDuration).
  • Groovy has Duck Typing.  So this: def futureDate = now + 10.years
  • ...is really this:   Date futureDate = TimeCategory.plus ((Date) now + ((BaseDuration)10).years())
  • BaseDuration has a field "years", and a method BaseDuration.getYears,  so now I can understand how the 10.years part works.  But it doesn't have a "year" property, or getYear() method, so I don't understand how 10.year works (and it does).  So would 10.months and 10.month, 10.weeks and 10.week, etc.
Anyway, that's what I would think:  But I'm wrong.  Because println of each of futureDate.class, now.class, and 10.class shows that they are in order, of class:  Date, Date, Integer.  So I'm even more confused than ever, and I don't know how this all is taking place...

On Fri, Mar 6, 2009 at 11:03 PM, Jean Barmash <[hidden email]> wrote:
Mike,

I am also fairly new to Groovy, and don't  understand categories too well, but from http://docs.codehaus.org/display/GROOVY/Groovy+Categories

There are many situations where you might find that it would be useful if a class not under your control had additional methods that you define. In order to enable this capability, Groovy implements a feature borrowed from Objective-C , called Categories. There are a few categories that are included in the system for adding functionality to classes that make them more usable within the Groovy environment.

1.Year comes from this method:
  http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/TimeCategory.html#getYear(java.lang.Integer)

getYear

public static DatumDependentDuration getYear(Integer self)
Cheers,

Jean


From: Mike Schwager <[hidden email]>
To: [hidden email]
Sent: Friday, March 6, 2009 11:35:13 PM
Subject: [groovy-user] Where does Integer get its year from in use(TimeCategory)?

Hi,
I have been studying a Groovy example:

use (org.codehaus.groovy.runtime.TimeCategory) {

def now = new Date()

def futureDate = now + 10.year

println "${now} => ${futureDate}"

}


...I don't understand where the Integer 10 gets the "year" method from.  It has something to do with TimeCategory, I know, because if I don't use() it, the code doesn't work. However, I am checking the Groovy API docs and in the TimeCategory class I can find no method or property "year".  (Note that 10.years will work above, as well, yet there is no such method or property in TimeCategory that I can find in the Groovy 1.6.0 docs.)


I need another hit with the cluestick.  Thanks.

-Mike Schwager



Reply | Threaded
Open this post in threaded view
|

Re: Where does Integer get its year from in use(TimeCategory)?

paulk_asert

Hi Mike, perhaps you might enjoy reading GinA. There are some
basic concepts that it would really help you with. Here are
some of the points that might help you understand this case.
They will be on the wiki too but perhaps harder to find.

Groovy supports syntactic sugar for the conventions used by
JavaBeans. With JavaBeans if you want the conceptual property
year, then you would create getYear() and setYear(year) methods.
By convention you might also have a backing field called
'year' but you could in fact call it anything you want.
Groovy supports these conventions by letting you access the
conceptual property name. If you type def x = foo.year then
Groovy converts that to x = foo.getYear() automatically. If you
type foo.year = x, then Groovy translates that into foo.setYear(x).
If Groovy doesn't find the get or set method, and you have no
metaprogramming in place, it will look for the field.

(Groovy also has the notion of properties which take this
concept further allowing syntactic sugar when defining your
own classes in Groovy - but let's not get distracted here.)

Now on to categories. For classic (i.e. ignoring the recently
added @Category annotation) categories like TimeCategory, Groovy
looks for methods of the form:

static ReturnType methodName(TargetType, args)

and if found creates a 'dynamic' instance method on TargetType like:

ReturnType methodName(args)

So, if you see in TimeCategory something like:

public static Duration getWeeks(final Integer self)

It does in fact add the following instance method to Integer:

public Duration getWeeks()

And as you now know because of the JavaBean syntactic sugar
is exactly what gets called when you refer to 10.weeks

Hope that helps.

Just on the doco, for most of the category methods we have both the
original JavaDoc form (api/gapi) and the 'translated into form'
(groovy-jdk) to save users of Groovy from having to even look at
the static methods. We don't have the translated version for
TimeCategory. It might be good if we did.

Cheers, Paul.

Mike Schwager wrote:

> Boy, I'm really having a hard time with some of these concepts.  On the
> face of it, everything is nice and there are lots of shortcuts that
> Groovy provides.  But when I really want to do something, I have an
> extremely hard time trying to understand how an author could have
> conjured up something.  I look at the api docs, and I see "getYear()"
> and "getYears()", but I don't see the attribute mentioned elsewhere in
> the doc- I need to just know that, "Hey Groovy programmer- there's a
> getYear() so there must be a year lurking in there!".  And there's no
> description of what TimeCategory does and how I might want to use it.
>  Throughout the documentation I have been reading, the authors seem to
> be these magicians conjuring up all kinds of neat stuff- "Ta-daa!  And
> look how easy that was!"  And it sure looks easy, until you start trying
> to wrap your mind around it.  Then, there's so much hocus-pocus taking
> place it twists the brain into a pretzel.
>
> Honestly- and I'm sorry if this sounds inflammatory- but when I wrote a
> Ruby CGI script for work a couple of weeks ago, I had a big
> webpage/table thing up in 2 days, and it was pretty easy from start to
> finish.  Now I'm working with Groovy trying to scan some data and
> convert it to XML, and I'm spending hours just trying to understand how
> Groovy is pushing Java around, and what I can use, how it all works
> together, so on and so forth.  I had something working pretty easily but
> then I needed to change gears and get into Closures and oh boy did I
> stick my foot in it.
>
> Anyway, here's what I said about your response.  I'm sure I'm wrong, but
> if it helps any of the Groovy gurus understand what some of us lesser
> mortals are going through, then maybe that's a good thing:
> ------------------------------------------------------------------------------------------
>
> I don't think that's correct.  I've looked it over some more, and
> thought about it.  A couple of things need to be explained:
>
>     * Where does Integer get a year method?  In the book "Programming
>       Groovy" (Subramaniam) p. 203, the author says "Groovy categories
>       provide a controlled way to inject methods".  So,
>     * In order to inject a method, you must have created the method.
>        Where is that done in this example?  Sure, the TimeCategory class
>       has a method getYear(), but 10 is an Integer
>     * How is 10.year then added to an object of Class Date?
>
> I think the explanation goes, in part, like this:
>
>     * Groovy has operator overloading.  So when you are adding a Date +
>       (what appears to be the Integer) 10, you are really doing:  
>       TimeCategory.plus(Date, BaseDuration).
>     * Groovy has Duck Typing.  So this: def futureDate = now + 10.years
>     * ...is really this:   Date futureDate = TimeCategory.plus ((Date)
>       now + ((BaseDuration)10).years())
>     * BaseDuration has a field "years", and a method
>       BaseDuration.getYears,  so now I can understand how the 10.years
>       part works.  But it doesn't have a "year" property, or getYear()
>       method, so I don't understand how 10.year works (and it does).  So
>       would 10.months and 10.month, 10.weeks and 10.week, etc.
>
> Anyway, that's what I would think:  But I'm wrong.  Because println of
> each of futureDate.class, now.class, and 10.class shows that they are in
> order, of class:  Date, Date, Integer.  So I'm even more confused than
> ever, and I don't know how this all is taking place...
>
> On Fri, Mar 6, 2009 at 11:03 PM, Jean Barmash <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Mike,
>
>     I am also fairly new to Groovy, and don't  understand categories too
>     well, but from http://docs.codehaus.org/display/GROOVY/Groovy+Categories
>
>     There are many situations where you might find that it would be
>     useful if a class not under your control had additional methods that
>     you define. In order to enable this capability, Groovy implements a
>     feature borrowed from Objective-C
>     <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_4_section_7.html>,
>     called Categories. There are a few categories that are included in
>     the system for adding functionality to classes that make them more
>     usable within the Groovy environment.
>
>     1.Year comes from this method:
>      
>     http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/TimeCategory.html#getYear(java.lang.Integer)
>     <http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/TimeCategory.html#getYear%28java.lang.Integer%29>
>
>
>           getYear
>
>     public static DatumDependentDuration <http://../../../../groovy/time/DatumDependentDuration.html> *getYear*(Integer <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html?is-external=true> self)
>
>     Cheers,
>
>     Jean
>
>     ------------------------------------------------------------------------
>     *From:* Mike Schwager <[hidden email] <mailto:[hidden email]>>
>     *To:* [hidden email] <mailto:[hidden email]>
>     *Sent:* Friday, March 6, 2009 11:35:13 PM
>     *Subject:* [groovy-user] Where does Integer get its year from in
>     use(TimeCategory)?
>
>     Hi,
>     I have been studying a Groovy example:
>
>     use (org.codehaus.groovy.runtime.TimeCategory) {
>
>     def now = new Date()
>
>     def futureDate = now + 10.year
>
>     println "${now} => ${futureDate}"
>
>     }
>
>
>     ...I don't understand where the Integer 10 gets the "year" method
>     from.  It has something to do with TimeCategory, I know, because if
>     I don't use() it, the code doesn't work. However, I am checking the
>     Groovy API docs and in the TimeCategory class I can find no method
>     or property "year".  (Note that 10.years will work above, as well,
>     yet there is no such method or property in TimeCategory that I can
>     find in the Groovy 1.6.0 docs.)
>
>
>     I need another hit with the cluestick.  Thanks.
>
>     -Mike Schwager
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Where does Integer get its year from in use(TimeCategory)?

Mike Schwager
Thanks, Paul.  I appreciate your indepth reply.

On Sat, Mar 7, 2009 at 5:20 PM, Paul King <[hidden email]> wrote:

Hi Mike, perhaps you might enjoy reading GinA. There are some
basic concepts that it would really help you with. Here are
some of the points that might help you understand this case.
They will be on the wiki too but perhaps harder to find.

Groovy supports syntactic sugar for the conventions used by
JavaBeans. With JavaBeans if you want the conceptual property
year, then you would create getYear() and setYear(year) methods.
By convention you might also have a backing field called
'year' but you could in fact call it anything you want.
Groovy supports these conventions by letting you access the
conceptual property name. If you type def x = foo.year then
Groovy converts that to x = foo.getYear() automatically. If you
type foo.year = x, then Groovy translates that into foo.setYear(x).
If Groovy doesn't find the get or set method, and you have no
metaprogramming in place, it will look for the field.

(Groovy also has the notion of properties which take this
concept further allowing syntactic sugar when defining your
own classes in Groovy - but let's not get distracted here.)

Now on to categories. For classic (i.e. ignoring the recently
added @Category annotation) categories like TimeCategory, Groovy
looks for methods of the form:

static ReturnType methodName(TargetType, args)

and if found creates a 'dynamic' instance method on TargetType like:

ReturnType methodName(args)

So, if you see in TimeCategory something like:

public static Duration getWeeks(final Integer self)

It does in fact add the following instance method to Integer:

public Duration getWeeks()

And as you now know because of the JavaBean syntactic sugar
is exactly what gets called when you refer to 10.weeks

Hope that helps.

Just on the doco, for most of the category methods we have both the
original JavaDoc form (api/gapi) and the 'translated into form'
(groovy-jdk) to save users of Groovy from having to even look at
the static methods. We don't have the translated version for
TimeCategory. It might be good if we did.

Cheers, Paul.

Mike Schwager wrote:
Boy, I'm really having a hard time with some of these concepts.  On the face of it, everything is nice and there are lots of shortcuts that Groovy provides.  But when I really want to do something, I have an extremely hard time trying to understand how an author could have conjured up something.  I look at the api docs, and I see "getYear()" and "getYears()", but I don't see the attribute mentioned elsewhere in the doc- I need to just know that, "Hey Groovy programmer- there's a getYear() so there must be a year lurking in there!".  And there's no description of what TimeCategory does and how I might want to use it.  Throughout the documentation I have been reading, the authors seem to be these magicians conjuring up all kinds of neat stuff- "Ta-daa!  And look how easy that was!"  And it sure looks easy, until you start trying to wrap your mind around it.  Then, there's so much hocus-pocus taking place it twists the brain into a pretzel.

Honestly- and I'm sorry if this sounds inflammatory- but when I wrote a Ruby CGI script for work a couple of weeks ago, I had a big webpage/table thing up in 2 days, and it was pretty easy from start to finish.  Now I'm working with Groovy trying to scan some data and convert it to XML, and I'm spending hours just trying to understand how Groovy is pushing Java around, and what I can use, how it all works together, so on and so forth.  I had something working pretty easily but then I needed to change gears and get into Closures and oh boy did I stick my foot in it.

Anyway, here's what I said about your response.  I'm sure I'm wrong, but if it helps any of the Groovy gurus understand what some of us lesser mortals are going through, then maybe that's a good thing:
------------------------------------------------------------------------------------------

I don't think that's correct.  I've looked it over some more, and thought about it.  A couple of things need to be explained:

   * Where does Integer get a year method?  In the book "Programming
     Groovy" (Subramaniam) p. 203, the author says "Groovy categories
     provide a controlled way to inject methods".  So,
   * In order to inject a method, you must have created the method.
      Where is that done in this example?  Sure, the TimeCategory class
     has a method getYear(), but 10 is an Integer
   * How is 10.year then added to an object of Class Date?

I think the explanation goes, in part, like this:

   * Groovy has operator overloading.  So when you are adding a Date +
     (what appears to be the Integer) 10, you are really doing:        TimeCategory.plus(Date, BaseDuration).
   * Groovy has Duck Typing.  So this: def futureDate = now + 10.years
   * ...is really this:   Date futureDate = TimeCategory.plus ((Date)
     now + ((BaseDuration)10).years())
   * BaseDuration has a field "years", and a method
     BaseDuration.getYears,  so now I can understand how the 10.years
     part works.  But it doesn't have a "year" property, or getYear()
     method, so I don't understand how 10.year works (and it does).  So
     would 10.months and 10.month, 10.weeks and 10.week, etc.

Anyway, that's what I would think:  But I'm wrong.  Because println of each of futureDate.class, now.class, and 10.class shows that they are in order, of class:  Date, Date, Integer.  So I'm even more confused than ever, and I don't know how this all is taking place...

On Fri, Mar 6, 2009 at 11:03 PM, Jean Barmash <[hidden email] <mailto:[hidden email]>> wrote:

   Mike,

   I am also fairly new to Groovy, and don't  understand categories too
   well, but from http://docs.codehaus.org/display/GROOVY/Groovy+Categories

   There are many situations where you might find that it would be
   useful if a class not under your control had additional methods that
   you define. In order to enable this capability, Groovy implements a
   feature borrowed from Objective-C    <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_4_section_7.html>,

   called Categories. There are a few categories that are included in
   the system for adding functionality to classes that make them more
   usable within the Groovy environment.

   1.Year comes from this method:
       http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/TimeCategory.html#getYear(java.lang.Integer)
   <http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/TimeCategory.html#getYear%28java.lang.Integer%29>


         getYear

   public static DatumDependentDuration <http://../../../../groovy/time/DatumDependentDuration.html> *getYear*(Integer <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html?is-external=true> self)

   Cheers,

   Jean

   ------------------------------------------------------------------------
   *From:* Mike Schwager <[hidden email] <mailto:[hidden email]>>
   *To:* [hidden email] <mailto:[hidden email]>

   *Sent:* Friday, March 6, 2009 11:35:13 PM
   *Subject:* [groovy-user] Where does Integer get its year from in
   use(TimeCategory)?

   Hi,
   I have been studying a Groovy example:

   use (org.codehaus.groovy.runtime.TimeCategory) {

   def now = new Date()

   def futureDate = now + 10.year

   println "${now} => ${futureDate}"

   }


   ...I don't understand where the Integer 10 gets the "year" method
   from.  It has something to do with TimeCategory, I know, because if
   I don't use() it, the code doesn't work. However, I am checking the
   Groovy API docs and in the TimeCategory class I can find no method
   or property "year".  (Note that 10.years will work above, as well,
   yet there is no such method or property in TimeCategory that I can
   find in the Groovy 1.6.0 docs.)


   I need another hit with the cluestick.  Thanks.

   -Mike Schwager





---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email