Wiki
Version 1 (modified by hopscc, 15 years ago)

--

Interface Definition

An interface defines a feature set or related functionality as a small group of properties, methods, and events.

A class or struct implementing an interface provides a concrete implementation for all of the members defined by that interface.

An Interfaces members are automatically public.

The interface itself provides no functionality (implementation) that a class or struct can inherit the way that base class functionality can be inherited. (Instead see mixins?)
However, if a base class implements an interface, any derived class inherits that implementation (and thus continues to implement that interface).

  • An interface is like an abstract base class:
    any non-abstract type inheriting the interface must implement all its members.
  • An interface cannot be instantiated directly.
  • Interfaces contain no implementation of methods or properties.
  • An Interface cannot contain fields.
  • Classes and structs can inherit from more than one interface.
  • An interface can itself inherit from multiple interfaces.

In Cobra, Interfaces are declared using the interface keyword followed by the interface name.
The interface may have any of the usual access modifiers and interface hierarchies (SuperInterfaces) can be specified using inherits

Interfaces can have attributes and may be declared as generic with constraints.

Use of a docstring on Interfaces is encouraged.

Interface Grammar

# The grammar elements below are ordered for simplicity but the compiler will accept 
# those elements in any reasonable order.

interface <InterfaceName>  
    [is <AccessModifiers>]
    [has <Attributes>]
    [where <GenericParam> must be <GenericConstraints>]
    [inherits <BaseInterfaceName> [, <BaseInterfaceName>]... ]
    [<DocString>]

    [ 
        [<Properties>]
        [<Methods>
        [<Events>]

Example

This example declares an interface, IDimensions, and a class, Box,
which explicitly implements the interface members getLength and getWidth.

The members are accessed through the interface instance dimensions.

interface IDimensions
    """ Interface for Dimensions of a 2D object"""
    def getLength as float
    def getWidth as float

class Box implements IDimensions
    var lengthInches as float
    var widthInches as float

    cue init length as float, width as float)
        .lengthInches = length
        .widthInches = width

    # Explicit interface member implementation: 
    def getLength as float
        return .lengthInches
        
    def getWidth as float
        return .widthInches

    def main is shared
        #// Declare a class instance box1:
        box1 = Box(30.0f, 20.0f)

        #// Declare an interface instance dimensions:
        dimensions = box1 to IDimensions

        #// Print out the dimensions of the box by calling the methods 
        #// from an instance of the interface:
        print 'Length: [dimensions.getLength]'
        print 'Width:  [dimensions.getWidth]'
    }
}
/# Output:
    Length: 30
    Width: 20
#/

Using generics

interface IEquatable<of T>
    """Interface for an object that can tell if it is equal to other objects of the same type"""
    def equals(obj as T) as bool


class Car implements  IEquatable<of Car>
    pro make as String
    pro model as String
    pro year as String

    #// Implementation of IEquatable<of T> interface
    def equals(car as Car) as bool
        if .make == car.make and .model == car.model and .year == car.year
            return true
        return false

Notes