Accessing a property or method when inheriting from RedefinesDot

2 ビュー (過去 30 日間)
Antonio Hortal
Antonio Hortal 2023 年 8 月 23 日
コメント済み: James Lebak 2023 年 9 月 11 日
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 件のコメント
James Lebak
James Lebak 2023 年 9 月 11 日
No. This is by design. RedefinesDot does not override properties that are accessible in the present context.

サインインしてコメントする。

採用された回答

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 Center および File ExchangeConstruct 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