Accessing a property or method when inheriting from RedefinesDot
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Antonio Hortal
am 23 Aug. 2023
Kommentiert: James Lebak
am 11 Sep. 2023
When inheriting from matlab.mixin.indexing.RedefinesDot, I have noticed that dot-indexed properties do not trigger the dotReference or dotAssign methods
A simple example:
classdef MyDotClass < matlab.mixin.indexing.RedefinesDot && ...
matlab.mixin.indexing.OverridesPublicDotMethodCall
properties
MyProperty = 1
end
methods (Access = protected)
function out = dotReference(this, idxOp)
out = [];
disp("Dot referencing [" + idxOp(1).Name + "]")
end
function out = dotAssign(this, idxOp)
out = [];
disp("Dot assigning [" + idxOp(1).Name + "]")
end
function n = dotListLength(this, idxOp, idxContext)
n = 1;
end
end
end
m = MyDotClass();
result = m.MyProperty; % result = 1; (And doesn't print)
result = m.RandomThings; % result = []; (And prints >> Dot Referencing [RandomThings]
Inheriting from matlab.mixin.indexing.OverridesPublicDotMethodCall solves the issue for when public methods are called, but not for properties. Also, this behaviour is different than with subsref and subsassign, which actually trigger when a property is used.
My question is: is there is a way to also capture properties in the dotAssign and dotReference?
1 Kommentar
James Lebak
am 11 Sep. 2023
No. This is by design. RedefinesDot does not override properties that are accessible in the present context.
Akzeptierte Antwort
Yash
am 30 Aug. 2023
You are correct in your observation that when inheriting from matlab.mixin.indexing.RedefinesDot, the dotReference and dotAssign methods are not triggered when accessing properties. This is because these methods are designed to work with indexing operations, not property access.
Property access in MATLAB is typically handled by the get and set methods. When you access a property using dot notation (e.g., obj.Property), MATLAB automatically calls the get method to retrieve the property value. Similarly, when you assign a value to a property (e.g., obj.Property = newValue), MATLAB calls the set method to perform the assignment.
If you want to capture property access and assignment, you should override the get and set methods in your class, like this:
classdef MyDotClass < matlab.mixin.indexing.RedefinesDot && ...
matlab.mixin.indexing.OverridesPublicDotMethodCall
properties
MyProperty = 1
end
methods
function value = get.MyProperty(this)
disp("Getting MyProperty");
value = this.MyProperty;
end
function this = set.MyProperty(this, value)
disp("Setting MyProperty");
this.MyProperty = value;
end
end
methods (Access = protected)
function out = dotReference(this, idxOp)
out = [];
disp("Dot referencing [" + idxOp(1).Name + "]")
end
function out = dotAssign(this, idxOp)
out = [];
disp("Dot assigning [" + idxOp(1).Name + "]")
end
function n = dotListLength(this, idxOp, idxContext)
n = 1;
end
end
end
With these modifications, you can capture property access and assignment in your class. When you access or assign the MyProperty property, the get and set methods will be called, and you can insert your custom logic there.
I hope this helps.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!