Lucee supports Interfaces which Components can implment multiple interfaces. Combined with the use of Mixins, Lucee Interfaces can help type check and document concise code.

Creating an Interface

Interfaces in Lucee are Components (files with the .cfc extension) but instead of defining a component, they define an interface:

Function definitions are placed within the interface just like in components, except the function bodies are not implemented.

Implementing an Interface

Once an interface is created, a normal Component can be created that implements the interface

Multiple Interfaces

Components can implement multiple interfaces by supplying a comma separated list for the implements attribute

Note: If implementing multiple interfaces, all function definitions in the interfaces must be identical. Lucee does not currently support method overloading.

Using Interface Type Checking

In Function Arguments

Function arguments can have a type annotation which checks that the object being passed in the argument conforms to the given interface.

As a Return Type

Functions can have an Interface as a return type to ensure that the value returned from the function call implements the interface.

The following example shows a function getObject() which will ensure that the return value implements IMyInterface

Type Checking Multiple Interfaces

In Lucee, it is not possible for a type annotation of a function argument or a return type, to specify that the value implements multiple interfaces. In order to defensibly guard that the function argument passed implments an interface, use additional guard assertions for each interface required.

This code above makes use of Lucee's component reflection capabilities of the getMetaData() function to check if the component implements the interface. The structKeyExists() function will throw an error if it does not find the interface.

results matching ""

    No results matching ""