필터 지우기
필터 지우기

Accessing a property or method when inheriting from RedefinesDot

조회 수: 8 (최근 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 CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by