Skip to end of metadata
Go to start of metadata

The concept of adding new methods and fields to an existing object outside of it's declaration.

Groovy adds a bunch of new methods to the existing JDK to "groovify" it. For example, each java.util.Collection gets an each method so that you can do:

Currently these things are hardcoded into the Groovy core implementation. I don't like hardcoding for several reasons, first of all it's ugly, which is a subjection opinion. Secondly it could be very, very useful to be able to add new methods to other non-JDK APIs. For example, you want to groovify some J2EE API like Servlets or JMS or you might want to groovify some open-source project.

I suggest some syntax like this:

The expression after extend class would be a type expression. A type expression selects one or more classes, and we would need to specify in detail how this would work. For example, in this case it specifies an interface and therefor it actually introduces the each method into all the implementations of java.util.List.

Also known as:

  • Static crosscutting
  • Introductions
  • No labels

2 Comments

  1. I just noticed, this is sort of a duplicated of Extending Classes.

  2. We might wanna allow a list of types for common stuff. e.g.

    which will make it easier to add helper methods to various disperse types at the same time