Blitz3D+ Language Reference

Interfaces (Extended mode)

Interfaces describe a named set of Method signatures that unrelated custom Types can implement. They are available only in Extended language mode. An Interface contains no fields, state, constructor, or Method bodies.

Interface Counter
    Method Add(amount)
    Method Current()
End Interface

Type PlainCounter Implements Counter
    Field total

    Method Add(amount)
        Self.total = Self.total + amount
    End Method

    Method Current()
        Return Self.total
    End Method
End Type

Each Interface Method is one signature on one line. It does not use End Method. Parameters cannot have default values, and Method New is not permitted. A concrete Method satisfies a contract when its name, return Type, parameter count, and parameter Types match exactly. Parameter names and defaults on the concrete Method are ignored. Extra concrete Methods are allowed.

Implements is explicit and accepts a comma-separated list. A matching Method without an Implements clause does not create an Interface conversion. A Type must satisfy every listed contract before executable statements are analysed.

plain:PlainCounter = New PlainCounter
counter:Counter = plain

counter.Add(3)       ; dynamically selects PlainCounter.Add
Print counter.Current()

plain.Add(2)         ; direct, statically selected Type Method call

An Interface value is the same object pointer as its concrete value. It may be stored in locals, globals, fields, basic arrays, fixed vectors, parameters, and return values. Assigning Null, comparing with Null, comparing two values of the same Interface, converting to String, and deleting one object through an Interface value are supported. The normal Blitz custom-Type reference and deletion rules still apply; Interfaces do not add garbage collection or automatic resource cleanup.

A concrete value converts only to an Interface named in its Implements clause. Interface-to-concrete, unrelated concrete-to-Interface, and cross-Interface conversions are rejected. Interface inheritance and runtime casts are not provided.

An Interface exposes only its declared Methods. It cannot be used with New, fields, Each, First, Last, Before, After, Insert, Delete Each, or Object handle casts. Interfaces have no common Type list, default Methods, reflection, overloading, inheritance, or Class hierarchy.

Extended Type Lists provide explicit, independently managed collections of Interface references without changing that rule. See Type Lists.

See the runnable Interface example and the longer shoot-'em-up enemy architecture guide.