How to Improve Interfaces

The post is a mix of Java, Scala and some fantasy language. I use the code examples to explain my points not as actual working code

I really like interfaces. I wrote about them here. They feel clean to me. They abstract ideas and minimize the contract between classes. I encourage developers to develop against interfaces.

Instead of depend on a broad class,

narrow the dependency down to

This even made it onto my list for better Java. But beside adding Interfaces to the language, Java makes it hard to use interfaces.

For easier and broader usage of interfaces, my two wishes for a language to implement would be

  1. Interfaces for attributes
  2. Classes that do not implement an interface can still be used

With both of these one would get most of the dynamic 'quack-like-a-duck' power combined with compile time type safety.
There is one downside to implicit matching interfaces. By declaring implements one declares that a class follows semantically an interface. Implicitly this may not be the case.

[edit: Found today in a Golang presentation about implicit interfaces: "It's a form of duck typing, but (usually) checkable at compile time. It's also another form of orthogonality."]

Classes that do not implement an interface can still be used

Let's start with number 2. The easy solution is for a class that implements all the method of an interfaces but does not declare that it implements the interface. For example:

I now wish to use a person object with the method defined above

In Scala this can be achieved with

- go try it yourself - but I'd wish the compiler would do this automatically (with caching the generated class) so I'd simply write

When modifying instances (create Person with Nameable, cast (?) instance to new class) this also works where one has no control over the creation of Person (but I'm no JVM specialist):

The harder case is when a class does not implement the method. For example a Person class

does not implement Nameable and does not provide the necessary method. So we would need a language construct to extend the class with the missing methods.

Alternative syntax could be

Another way would be to extend the Person class with extension methods, like Kotlin extends classes for example, until it satisfies the Interface. Then the automatic part above can apply.

Go implements this in a nice way:

Both would make it much easier to work with Interfaces.

Interfaces for Attributes

I'm not a fan of getter and setters in Java. They add boilder plate code all the time and only very seldom I've encountered a problem that was solved by getter and setters. One argument for setters is value validation. I prefer to use validated types in this case with no need for a validating setter.

could be implemented by

and be used in a method

Interfaces for attributes might be controversial, but in languages with data classes or properties this could be very useful. I miss interfaces for attributes all the time in my Scala coding and I guess this happens to others as well in languages that do not push getters and setters like Java.

These two extensions to the power of Interfaces I'd love to see in a programming language. If I have more thoughts about interfaces I'll add to this blog post. The thoughts are also a base for my own programming language experiment.

How Scala can do this

There are some ways in Scala to bring Person and Nameable together. On way is with implicit classes (although I had to implement the method).

In Scala one might use Type Classes for implementing Nameable. Chris ( @cbirchall ) was kind enough to supply a gist

A third option is with Scala structural types.

The downside are - alledged - performance problems with structural types and more important it does not play as nicely with Interfaces as an automatic approach.

If you have ideas about interfaces or general feedback, reply to @codemonkeyism

Stephan the codemonkey writes some code since 35 years, was CTO in some companies and currently helps startup with their technical challenges