I just upgraded one of my Grails apps to 2.2.0 which is using Groovy 2.0 and I am now getting this compile error:
Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.
Based on the following code:
def getRootDomain(key) { }
private getRootDomain() { }
It's an easy fix but I'd really like to understand the why better. Can someone explain this to me?
Both protected and private methods cannot be called from the outside of the defining class. Protected methods are accessible from the subclass and private methods are not. Private methods of the defining class can be invoked by any instance of that class. Public access is the default one.
Generally you should expose as little as possible and make everything private that is possible. If you make a mistake and hide something you should be exposing, no problem, just make it public.
However, although the syntax allows private classes to have public methods, it won't increase the visibility of those methods sufficiently to be visible outside the containing class.
Yes it is very bad practice - you're letting your tools make design decisions for you. I think the main problem here is that you're trying to treat each individual method as a unit. This is generally the cause of all unit test woes.
It's because of the way groovy chooses which method to call, and its potential incompatibility with java.
Have you seen this thread on the mailing list?
http://groovy.329449.n5.nabble.com/mixing-public-private-overloaded-methods-causes-compilation-error-td367147.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With