Accessing a property or method when inheriting from RedefinesDot

2 次查看(过去 30 天)
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?

采纳的回答

Yash
Yash 2023-8-30
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.
  1 个评论
Antonio Hortal
Antonio Hortal 2023-8-30
Thanks a lot @Yash for the reply!
I'd argue that if there's a way to override method calls it (inheriting from matlab.mixin.indexing.OverridesPublicDotMethodCall), it would also make kind of sense to have a similar way to override property referencing and assignment. But I get this would be another that is already covered with the property getters and setters.
I will stick with subsref and subsasgn for now :)
Thanks again

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by