[
https://issues.apache.org/jira/browse/GROOVY-8722?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16565099#comment-16565099
]
Paul King commented on GROOVY-8722:
-----------------------------------
You are correct that currently final is ignored/not supported and it isn't
mentioned in the docs. We should definitely add something in the docs and/or a
compiler error message.
While not an exact equivalent, it is worth reading the "api evolution"
rationale about why Java doesn't allow methods in interfaces to be final:
https://stackoverflow.com/a/23476994/146084
Now, traits are a slightly more powerful beast and you could make an argument
about them having a different semantics here, but similar arguments still do
apply. It is worth going back to the intent of traits to consider whether
supporting final would make sense. Currently, a method in a trait provides a
default implementation that can be overridden. So, if you provide your own
implementation does that mean you are overriding the trait method or just not
taking advantage of a default implementation?
If you multiply inherit a method from multiple traits, the last one "inherited"
wins. So if an earlier one is final, should I give a warning or just realise
that I am not taking that implementation? And, if I am going to ignore an
earlier final method in that case, does it also make sense to ignore such
"inherited " methods if I provide my own implementation?
Probably the only definition that would make sense would be that if you inherit
one final method you can't inherit any others (with the same signature) nor
override. If we did support final, that is probably the definition that I would
advocate but we need to appreciate that it would be the definition that
impacted api evolution the most.
> final modifier for non-abstract methods in traits is ignored
> ------------------------------------------------------------
>
> Key: GROOVY-8722
> URL: https://issues.apache.org/jira/browse/GROOVY-8722
> Project: Groovy
> Issue Type: Bug
> Components: Compiler, Documentation
> Affects Versions: 2.4.15, 2.5.1
> Reporter: Dmytro Buryak
> Priority: Major
> Labels: documentation
>
> {{When use {color:#707070}final{color} modifier in trait non-abstract method
> signature, then:}}
> * {{compiler successfully compiles trait and class that implements it}}
> * {{final method of trait may be overridden in class that implements trait}}
> * {{if class implements trait and doesn't override trait final method, then
> this method is available as non-final method in class}}
> * {{documentation says absolutely nothing about this}}
> {{In other words, {color:#707070}final{color} modifier is ignored in trait
> methods: code works the same with or without it. Even if this behavior is
> expected, there's nothing about it in the documentation.}}
> {{Here's simple example gradle groovy project to demonstrate the issue:
> [https://github.com/dburyak/groovy-bug-trait-final|http://example.com]}}
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)