groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paolo Di Tommaso <paolo.ditomm...@gmail.com>
Subject Re: Add a marker interface to bypass Collections and Maps formatting
Date Tue, 31 May 2016 09:36:48 GMT
Hello guys,

No feedback on this? Would you take in consideration a PR for this
proposal?


Thanks,
Paolo


On Sat, May 28, 2016 at 6:26 PM, Paolo Di Tommaso <paolo.ditommaso@gmail.com
> wrote:

> Hi all,
>
> Groovy implements a built-in formatting strategy for collection and map
> objects that is surely nicer and more useful than the one provided by the
> default Java implementation for these classes.
>
> However there are use cases in which custom collection or map classes need
> to implement their own formatting rule.
>
> Currently in Groovy this is quite painful and may lead to inconsistent
> results. Take in consideration the following example:
>
> class MyList extends ArrayList {
>  String toString() {
>     this.join('-')
>   }
> }
>
> def x = new MyList()
> x << 1 << 2 << 3
>
> println x.toString()
> println x
> println "$x"
>
> Which prints:
>
> 1-2-3
> [1, 2, 3]
> [1, 2, 3]
>
> Both the second and third `println` use the Groovy built-in formatting
> method and there's no easy way to override this behaviour. Also there's not
> a clear reason why the first and the second print return a different
> output.
>
> The only options I've found is to define `MyList` with a @Delegate without
> implementing the `List` interface. But this leads to other weird side
> effects. The remaining possibility is to use some bytecode manipulation to
> bypass the default Groovy formatting, but it looks to me a really
> overkilling solution for such problem.
>
> For this reason a would like to propose to introduce a mechanism that
> would allow custom collection and map classes to bypass the default
> formatting method. This should not be too difficult. The current Groovy
> built-in formatting is implemented by formatList
> <https://github.com/apache/groovy/blob/master/src/main/org/codehaus/groovy/runtime/InvokerHelper.java#L678-L712>
> and formatMap
> <https://github.com/apache/groovy/blob/master/src/main/org/codehaus/groovy/runtime/InvokerHelper.java#L641-L668>
> methods.
>
> It would be enough to add a marker interface (or an annotation) that when
> applied to a class it would be used to by-pass the logic in the formatList
> and formatMap methods and simply return the string provided by the object
> `toString` method.
>
>
> I could easily contribute this patch however I would know the opinion of
> the Groovy core committers. In particular:
>
> 1) What name should have this marker interface? groovy.lagn.Something?
> 2) Are formatList and formatMap methods the right place to add this logic?
> 3) A similar problem exists also when using the `equals` (and hashCode?)
> method for collections and maps. Should this mechanism be extended also to
> this case?
>
>
>
> Best,
> Paolo
>
>
>

Mime
View raw message