Objects in Scilab

Objects in Scilab: classes, instantiation, and multiple inheritance

Why it matters

Until now, object-oriented patterns were often simulated with mlist/tlist and conventions. Objects make these use cases clearer, more robust, and easier to maintain, and they help structure codebases.

What ships in Scilab 2026.0

  • Classes with properties, methods and enumeration.

  • Constructors for instance initialization

  • Member visibility (public / protected / private)

  • Multiple inheritance (well-defined and documented method resolution)

Existing mechanisms (mlist/tlist, overloads) remain available. This is an addition to the language, not a breaking replacement.

Quick start

Declare a class and use it

This example introduces a minimal Point class.

classdef Point
    properties
        x = 0;
        y = 0;
    end
    methods
        function Point(x, y)
            this.x = x;
            this.y = y;
        end
        function r = norm()
            r = sqrt(this.x^2 + this.y^2);
        end
    end
end

p = Point(3, 4);
p.norm()    // 5
norm(p)     // 5

The properties block defines fields with default values, and the methods block holds both the constructor (a method named exactly like the class) and regular methods. Inside methods, this refers to the current instance and is implicitly available, you don’t pass it in the signature. The norm method computes the Euclidean length of the point. You can call it in two equivalent ways: instance style (p.norm()) or functional style (norm(p)). Scilab dispatches norm(p) to Point.norm when the first argument is a Point, so both lines return 5 for the (3,4) example.

Simple encapsulation

This example demonstrates encapsulation with a private field.

classdef Counter
    properties (private)
        n = 0;
    end
    methods
        function inc()
            this.n = this.n + 1;
        end

        function v = value()
            v = this.n;
        end
    end
end

c = Counter();
c.n = 10 // Wrong insertion: property 'n' is not accessible.
c.inc();  // +1
inc(c);  // +1
c.value() // 2

The counter’s state n is declared private, so only code inside Counter can read or modify it, accessing c.n directly (or doing c.n = 10) is not allowed. All state changes go through methods: inc() increments the counter, and value() returns it.

Multiple inheritance

This example shows multiple inheritance with a lightweight mixin.

classdef ColorMixin
    properties
        color = "red";
    end
end

classdef ColoredPoint < Point & ColorMixin
    methods
        function ColoredPoint(x, y)
            Point(x, y);
        end
        function s = string()
            s = msprintf("(%d, %d) [%s]", this.x, this.y, this.color);
        end
        function disp()
            disp(string(this));
        end
    end
end

cp = ColoredPoint(1,2);
cp // (1, 2) [red]

ColorMixin contributes the color property, while ColoredPoint < Point & ColorMixin combines the geometric behavior of Point with the mixin’s attribute. The ColoredPoint constructor delegates to Point(x, y) to initialize coordinates. Member lookup gives you x/y from Point and color from ColorMixin. The string() method defines a textual representation, and the disp() override forwards to string(this), so printing the instance (or just evaluating cp) renders "(1, 2) [red]". If multiple bases declare the same member, Scilab’s method-resolution order decides which one wins; there’s no conflict in this example.

Overloads

This example demonstrates operator overloading.

classdef Point2
    properties
        x
        y
    end

    methods
        function Point2(x, y)
            this.x = x;
            this.y = y;
        end

        // Overload operation double + Point2 or Point2 + double
        function r = plus_s(a, b)
            if isa(a, "Point2") then
                r = Point2(a.x + b, a.y + b);
            else
            r = Point2(a + b.x, a + b.y);
            end
        end

        // Overload operation Point2 + Point2
        function r = plus_Point2(a, b)
            r = Point2(a.x + b.x, a.y + b.y);
        end

        // Overload for all other + operation with Point2
        function r = plus(a, b)
            error("operation not managed.");
        end

        function s = string()
            s = sprintf("(%d %d)", this.x, this.y);
        end

        function disp()
            disp(string(this));
        end
    end
end

p1 = Point2(3, 4)   //(3 4)
p2 = Point2(-3, 4)  //(-3 4)
p1 + p2             //(0, 8)
p1 + 4              //(7 8)
3 + p2              //(0 7)
p1 + "f"            //error

Overloads are resolved by method name: plus_Point2(a,b) handles Point2 + Point2, plus_s(a,b) covers the mixed scalar case (either Point2 + double or double + Point2), and the bare plus(a,b) acts as a catch-all fallback. In plus_s, the isa(a,"Point2") check lets one implementation support both operand orders. Each overload returns a new Point2 with component-wise addition. If no suitable overload exists (e.g., p1 + "f"), the generic plus raises an error.

Documentation

For more details, you can read documentation about Objects in Scilab.

Scilab Team

2 Likes

Good news for Scilab. I’ve been waiting for this type of improvement for a long time. I’m starting to create a toolbox for internal use at my company, and I’m wondering how to integrate and make these objects and class definitions available in my toolbox.