Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Incompatible types in interface polymorphism

This error occurs when an assignment or call argument involves an interface type but the source type does not satisfy the interface contract. There are two cases:

Case 1: POU does not implement the interface

When assigning a concrete function block or class instance to an interface-typed variable (or passing it as an argument), the POU must implement that interface — either directly via IMPLEMENTS or transitively through its EXTENDS chain.

Invalid

INTERFACE IA
    METHOD foo END_METHOD
END_INTERFACE

FUNCTION_BLOCK FbX
END_FUNCTION_BLOCK

FUNCTION main
    VAR
        instance : FbX;
        ref      : IA;
    END_VAR
    ref := instance;  // Error: FbX does not implement IA
END_FUNCTION

Valid

INTERFACE IA
    METHOD foo END_METHOD
END_INTERFACE

FUNCTION_BLOCK FbA IMPLEMENTS IA
    METHOD foo END_METHOD
END_FUNCTION_BLOCK

FUNCTION main
    VAR
        instance : FbA;
        ref      : IA;
    END_VAR
    ref := instance;  // OK: FbA implements IA
END_FUNCTION

Implementation is also satisfied transitively through inheritance:

FUNCTION_BLOCK FbA IMPLEMENTS IA
    METHOD foo END_METHOD
END_FUNCTION_BLOCK

FUNCTION_BLOCK FbB EXTENDS FbA
END_FUNCTION_BLOCK

FUNCTION main
    VAR
        instance : FbB;
        ref      : IA;
    END_VAR
    ref := instance;  // OK: FbB inherits IA implementation from FbA
END_FUNCTION

When assigning one interface-typed variable to another, the source interface must be the same as or extend the target interface. Downcasts (parent to child) and assignments between unrelated interfaces are rejected.

Invalid — downcast

INTERFACE IA
    METHOD foo END_METHOD
END_INTERFACE

INTERFACE IB EXTENDS IA
    METHOD bar END_METHOD
END_INTERFACE

FUNCTION main
    VAR
        refIA : IA;
        refIB : IB;
    END_VAR
    refIB := refIA;  // Error: cannot downcast IA to IB
END_FUNCTION

Invalid — unrelated interfaces

INTERFACE IA
    METHOD foo END_METHOD
END_INTERFACE

INTERFACE IB
    METHOD bar END_METHOD
END_INTERFACE

FUNCTION main
    VAR
        refIA : IA;
        refIB : IB;
    END_VAR
    refIA := refIB;  // Error: IB and IA are not related
END_FUNCTION

Valid — upcast

INTERFACE IA
    METHOD foo END_METHOD
END_INTERFACE

INTERFACE IB EXTENDS IA
    METHOD bar END_METHOD
END_INTERFACE

FUNCTION main
    VAR
        refIA : IA;
        refIB : IB;
    END_VAR
    refIA := refIB;  // OK: IB extends IA
END_FUNCTION