AS4 Feature proposal: Implied interfaces

! Warning: this post hasn't been updated in over three years and so may contain out of date information.

AS4 draftIdeas come and go and fashions change in software. One factor has however remained constant and unquestionably true throughout the 20+ years I’ve been writing code in a professional capacity. It is the mantra: “code should be highly cohesive and loosely coupled”. Cohesiveness has been neatly handled for many years through modularizing ones code. Just about every language I can think of (with the sole exception of VBScript) supports the idea of splitting functionality across multiple files, libraries, modules etc.

Loose coupling though has been a far harder nut to crack. Only recently with the surge in interest in the Dependency Injection and Inversion of Control (IoC) patterns, coupled with the use of interfaces has the issue started to be properly addressed in the OO world. Key to this is creating interfaces for all the coupling classes within a solution. These interfaces tend to contain boilerplate code. Consider the SimpleCommand and iCommand class and interface in PureMVC for example. PureMVC is far from being an example of IoC, but it makes good use of decoupling through the use of interfaces:

ICommand.as (with comments removed)

SimpleCommand.as (with comments removed)

Creating this style of interface is a really good idea: they aid testing and help decouple classes. However they are a nuisance to write. My proposal therefore is for a simple addition to AS3: implied interfaces. The above two files are replaced by just the one:

By changing “implements” to “implies”, we switch the compiler from expecting an interface, which the class must implement, into the class implying an interface through its public properties and methods. There is then no need to write the interface.

Taking a more abstract example, the following class:

would yield an implied interface with the following form:

This implied paradigm would be designed to handle just boilerplate interfaces. In situations where a more complex interface is required, they would of course still be explicitly defined. However in many cases, a one to one relationship between interface and class exists, and implied interfaces would save a lot of boilerplate coding in such situations.

Posted in AS4

3 thoughts on “AS4 Feature proposal: Implied interfaces

  1. Tacit definition of interfaces instead of explicit. I like the idea of this.

    Actually, I would go a step further and suggest the ability to have tacit implementation as well.

    Imagine you have a method which operates on things which are plottable (ie have x & y properties) You might make a nice IPlottable interface and implement it in all your custom graphic classes. But the problem is, you can’t change the MovieClip class, so there is no way to be able to pass a MovieClip straight to a method expecting an IPlottable – even though MovieClip implements the IPlottable interface tacitly.

    It would be nice if you could pass a MovieClip where an IPlottable is expected, and the compiler just checks to see if the required methods are there, instead of erroring because it has not been implemented explicitly.

    Tacit implementation of interfaces. Tacit definition of interfaces. Why not?

  2. Dear “Anonymous”, you are exactly wrong. PureMVC is an example of the Service Locator pattern and thus is the complete opposite of IoC.

Comments are closed.