Main Content

matlab.mixin.indexing.ForbidsPublicDotMethodCall class

Package: matlab.mixin.indexing

Disallow calling public methods using dot notation

Description

Classes that inherit from matlab.mixin.indexing.RedefinesParen or matlab.mixin.indexing.RedefinesBrace allow method calls using dot notation. To make such a class mirror the behavior of built-in numeric classes, which disallow method calls with dot, use the matlab.mixin.indexing.ForbidsPublicDotMethodCall superclass. For example, obj is an instance of a class that inherits from RedefinesParen and ForbidsPublicDotMethodCall. The class defines a public method myMethod:

  • myMethod(obj) calls the method.

  • obj.myMethod errors.

  • label="myMethod"; obj.(label) errors.

  • obj(1).myMethod calls parenReference.

  • label="myMethod"; obj(1).(label) calls parenReference.

ForbidsPublicDotMethodCall cannot be used with classes that inherit from matlab.mixin.indexing.RedefinesDot or matlab.mixin.indexing.OverridesPublicDotMethodCall.

Class Attributes

Abstract
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Examples

collapse all

The ArrayWithLabelNoDotMethod class inherits from RedefinesParen and ForbidsPublicDotMethodCall. (To see a full explanation of the ArrayWithLabel class without the use of ForbidsPublicDotMethodCall, see matlab.mixin.indexing.RedefinesParen.)

The ArrayWithLabelNoDotMethod class has two properties: ContainedArray and Label. The ArrayWithLabelNoDotMethod class customizes parentheses indexing into ContainedArray by inheriting from matlab.mixin.indexing.RedefinesParen and implementing all of its abstract methods.

ArrayWithLabelNoDotMethod also provides two public methods:

  • value — Displays the indexed values of ContainedArray

  • sum — Calculates the sum of the indexed values of ContainedArray

classdef ArrayWithLabelNoDotMethod < ...
    matlab.mixin.indexing.RedefinesParen & ...
    matlab.mixin.indexing.ForbidsDotMethodCall

    properties (Access=private)
        ContainedArray
    end
    
    properties (Access=public)
        Label
    end
    
    methods
        function obj = ArrayWithLabelNoDotMethod(val)
            obj.ContainedArray = val;
        end
    end

    methods (Access=protected)
        function varargout = parenReference(obj,indexOp)
            obj.ContainedArray = obj.ContainedArray.(indexOp(1));
            if isscalar(indexOp)
                varargout{1} = obj;
                return;
            end
            % Syntax for forwarding indexing operations
            [varargout{1:nargout}] = obj.(indexOp(2:end));
        end

        function obj = parenAssign(obj,indexOp,varargin)
            if isscalar(indexOp)
                assert(nargin==3);
                rhs = varargin{1};
                obj.ContainedArray.(indexOp) = rhs.ContainedArray;
                return;
            end
            [obj.(indexOp(2:end))] = varargin{:};
        end

        function n = parenListLength(obj,indexOp,ctx)
            if numel(indexOp) <= 2
                n = 1;
                return;
            end
            containedObj = obj.(indexOp(1:2));
            n = listLength(containedObj,indexOp(3:end),ctx);
        end

        function obj = parenDelete(obj,indexOp)
            obj.ContainedArray.(indexOp) = [];
        end
    end

    methods (Access=public)
        function out = value(obj)
            out = obj.ContainedArray;
        end
        
        function out = sum(obj)
            out = sum(obj.ContainedArray,"all");
        end
        
        function out = cat(dim,varargin)
            numCatArrays = nargin-1;
            newArgs = cell(numCatArrays,1);
            for ix = 1:numCatArrays
                if isa(varargin{ix},'ArrayWithLabel')
                    newArgs{ix} = varargin{ix}.ContainedArray;
                else
                    newArgs{ix} = varargin{ix};
                end
            end
            out = ArrayWithLabel(cat(dim,newArgs{:}));
        end

        function varargout = size(obj,varargin)
            [varargout{1:nargout}] = size(obj.ContainedArray,varargin{:});
        end
    end

    methods (Static, Access=public)
        function obj = empty()
            obj = ArrayWithLabel([]);
        end
    end
end

Construct an ArrayWithLabelNoDotMethod object with a 2-by-2 matrix, and assign a string to the Label property.

a = ArrayWithLabelNoDotMethod([2 3; 5 7]);
a.Label = "primes"
a = 

  2×2 ArrayWithLabelNoDotMethod array with properties:

    Label: "primes"

Display the array using the value method. Call value without using dot notation.

value(a)
ans = 

     2     3
     5     7

Make the same method call using dot notation. Because this class inherits from ForbidsPublicDotMethodCall, MATLAB® errors.

a.value
Dot method call is not supported for objects of type 'ArrayWithLabelNoDotMethod'.
Consider using function call syntax.
Introduced in R2021b