[ 
https://issues.apache.org/jira/browse/GROOVY-11143?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18133684#comment-18133684
 ] 

Christopher Smith commented on GROOVY-11143:
--------------------------------------------

I'm re-bumping this in regard to the recent mailing-list discussion. My 
business applications (using Gremlin, specifically because we can use 
Java/Groovy code to build semantic queries as an AST, and then Spring MVC with 
standardized handling pipelines) would benefit substantially from the ability 
to write DSL fragments alongside the business objects. We use _almost_ 
exclusively static mode.

I will make a napkin-drawing suggestion: Introduce the {{import static 
extension}} syntax suggested above. When this is encountered, apply a 
delegating {{ExtensionMethodCache}} that adds exactly the listed methods for 
that {{SourceUnit}}.

> Add extension method to source unit (same locality as static import)
> --------------------------------------------------------------------
>
>                 Key: GROOVY-11143
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11143
>             Project: Groovy
>          Issue Type: New Feature
>          Components: groovy-jdk
>            Reporter: Eric Milles
>            Priority: Minor
>
> I have been poking around with the many ways to extend a class/object's 
> functionality within groovy.  To add a new method that can be called like any 
> other instance method, there are a few possibilities:
> 1. modify the MetaClass
> {code:groovy}
> String.metaClass.ext = { ->
>   print delegate
> }
> "works".ext()
> {code}
> 2. use a category class
> {code:groovy}
> @Category(String) class Cat {
>   void ext() {
>     println this
>   }
> }
> use (Cat) {
>   "works".ext()
> }
> {code}
> 3. apply a mixin
> first create class similar to Cat in (2)
> {code:groovy}
> def str = "works"; str.metaClass.mixin(Cat)
> str.ext()
> {code}
> 4. apply a trait
> {code:groovy}
> trait Ext {
>   void ext() { println this.toLowerCase() } // see GROOVY-11142
> }
> "works".withTraits(Ext).ext()
> {code}
> 5. create an extension moodule
>  - create class similar to Cat in (2)
>  - create META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule file
>  - build and deploy separately from current script / project
> What I'm after is something that works as easily in a script or source file 
> as static imports but lets me call like an extension method.  And does not 
> modify state outside of the current script (like {{MetaClass}}) or introduce 
> a thread barrier (like {{use}}).  And works with static type checking.
> Scala has an "extension" mechanism that is similar to static import but there 
> is a keyword on the category class that lets compiler know the method is used 
> like any other instance method.  Scala used to provide "implicit" methods -- 
> I've lost my familiarity with those.
> I've seen "import static extension foo.Bar.baz;" for some language.  
> (citation required)
> And lastly, Kotlin provides a local extension like this:
> {code}
> fun foo.Bar.baz() { x ->
>   // ...
> }
> {code}
> For any new users or just to add something quickly that can be used in a 
> file, I think I'm looking for an enhancement to "import static ..." that will 
> get substituted in the source unit, like {{StaticImportVisitor}} does today 
> for static method calls.  For example:
> {code:groovy}
> import static foo.Bar.baz
> import static /*something*/ foo.Bar.baz
> baz("") // supported by first import
> "".baz() // supported by second import
> {code}
> I have been considering whether "extension", "default", some annotation or 
> something else should be used for /\*something\*/ in the script above.
> Similar to Kotlin, this could let someone declare an extension in a single 
> script with no dynamic runtime impact:
> {code:groovy}
> import static /*something*/ ThisScript.*
> static ext(String self) {
>   print self
> }
> "works".ext()
> {code}
> Has anyone looked at something like this in Groovy or other languages and has 
> some feedback?



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to