If You are Unfamiliar with OOP Matlab
For those not familiar with matlab, matlab provides a variety of access attributes when creating member variables. All member variables are essentially C# properties in their own right, who's get and set values default to normal access and assignment. Any (and I mean any) member variable can have its get and set "operators" overwritten like so:
%typical OOP class
classdef MyClass
%both Properties in the C# sense *and* member variables
%default access is public, put it here for clarity
properties (Access = public)
myProperty
myMember
end
methods
function self = set.myProperty(self, value)
% doesn't invoke recursion, actually references a variable called
% self.myProperty, which if you overwrite the get.property,
% you can't actually access outside of set and get
self.myProperty = ...
end
function value = get.myProperty(self)
...
end
%constructor, self is the class instance
function self = MyClass(value)
self.myMember = value;
%actually calls set.myProperty
self.myProperty = value;
end
end
end
You can't call get.myProperty or set.myProperty either, it isn't in the language at all.
If you want a real C# property you have to do the following, as at times you can't refer to the real value used inside the myProperty
functions if you have set defined(as you can only refer to assignment of that value inside that function) or get (where it would always call the get function).
classdef MyClass
%both Properties in the C# sense *and* member variables
%default access is public, put it here for clarity
properties (Access = private)
myMember
end
properties (Access = public, Dependent = true)
myProperty
end
methods
function self = set.myProperty(self, value)
% since the property is "dependent" it no longer can refer to a
% variable called self.myProperty, it can only refer to other properties
self.myMember = ...
end
function value = get.myProperty(self)
value = self.myMember
end
end
end
Matlab Property Attribute Background
Now I'm interested in specifically the following property combinations: Access
, GetAccess
, SetAccess
, and Depedent
:
Access which defines if the property is public, private or protected [1]
(Access = public|private|protected)
Get/Set propertes (this works like access above, but with get and set functions)[1]
(GetAccess = public|private|protected)
(SetAccess = public|private|protected)
Dependent (doesn't actually have a value directly associated with it, can't assign to its name inside of its get and set, looks like a C# property afterward, but can be private and protected as well)
(Dependent = true|false)
Question
Would a property with both get and set defined that wasn't Dependent
ever be a valid use case? It seems like you are hiding the value itself from the class this way, you are making the value an implementation detail to your own class. What about properties with either get
or set
defined but not both?
Would the validity change if the Set/GetAccess
or general Access
changed to private
, protected
or public
?
On the same line, should those properties with redefined get
and set
ever be used in implementation code? You have no clue what they actually do and to a casual observer they just look like normal members, so it would seem like their use would be dangerous in class implementation code, it seems like this would also necessitate a separate naming convention.
[1] (Note it can also define specific classes that have access to the property that are unrelated, the validity of this is outside the scope of the question).
1 Answer 1
I'm not a Matlab expert, but it seems to me that you're right; not all possible combinations of GetAccess
, SetAccess
, Access
and Dependent
are going to be valid.
For example, it doesn't make sense to me that a Dependent == true
property (which, if I understand it correctly, is a "calculated property" or "Always computed on the fly") would ever have a public
setter, since it would never make sense to allow a user to set a property that is being calculated internally. Even if you could successfully code it, there would be no place available for the incoming value to be stored.
Since the compiler may not have the intelligence to determine whether or not what you're attempting is reasonable, the onus is on the programmer to use combinations of these keywords that make sense.
This is just as true of C# as it is of Matlab. As C# developers gain experience, they come to understand certain combinations of these modifiers as being indicative of certain techniques.
For example:
public string Name { get; private set }
is immediately recognized by most C# developers as being part of a technique for making an object immutable, while other combinations, such as
public string Name { private get; private set }
are actually recognized by the compiler as being invalid.
Explore related questions
See similar questions with these tags.