March 29, 2017
On Wednesday, March 29, 2017 10:08:02 abad via Digitalmars-d-learn wrote:
> Related question, it seems that final methods are allowed in interfaces. Obviously you can't implement them anywhere, so is this also on purpose and on what rationale? :)

If the function is final, it can have an implementation.

interface I
{
    final bool foo() { return true; }
}

class C : I
{
}

void main()
{
}

- Jonathan M Davis
March 29, 2017
On Wed, Mar 29, 2017 at 11:24:04AM -0700, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Wednesday, March 29, 2017 10:08:02 abad via Digitalmars-d-learn wrote:
> > Related question, it seems that final methods are allowed in interfaces. Obviously you can't implement them anywhere, so is this also on purpose and on what rationale? :)
> 
> If the function is final, it can have an implementation.
[...]

If a function is final, it *must* have an implementation, since there can be no further overrides that would provide one in a derived type.

The rationale for allowing final methods in an interface is to provide users of the interface with nice syntactic sugar, e.g., a set of methods that are commonly used together abstracted into a single final method, while requiring subclasses to only implement a smaller number of orthogonal methods that can be used to implement that method.


T

-- 
When solving a problem, take care that you do not become part of the problem.
March 30, 2017
On Wednesday, 29 March 2017 at 09:50:10 UTC, abad wrote:
> 
> Is this on purpose and what's the rationale?

In Andrei's book, chapter 6.9.1 "the non virtual interface (NVI) idiom" answers your question. It cites this article by Herb Sutter as the originator of the idea:

http://www.gotw.ca/publications/mill18.htm
March 30, 2017
On Wednesday, 29 March 2017 at 10:08:02 UTC, abad wrote:
> Related question, it seems that final methods are allowed in interfaces. Obviously you can't implement them anywhere, so is this also on purpose and on what rationale? :)

That is not necessarily true. Final doesn't imply it can't be implemented. It implies that it cannot be override.

1 2
Next ›   Last »