MATLAB Answers

Giles
0

Loren says class properties can be the same as keywords but how?

Giles
さんによって質問されました 2018 年 10 月 26 日
最新アクティビティ Giles
さんによって 回答されました 2018 年 10 月 26 日
In this article https://blogs.mathworks.com/loren/2012/07/16/who-what-why-but-not-this/ near the bottom Loren says that class properties can be the same as keywords. However, how is this possible? If you write a classdef script any attempt to use a keyword (including class keywords like "events") in the properties block gets a red syntax error. Was she mistaken? I'm asking because I really want a property name that happens to be a keyword for a particular application.

  0 件のコメント

サインイン to comment.

タグ

製品


リリース

R2018b

2 件の回答

回答者: Philip Borghesani 2018 年 10 月 26 日

Yes they can be but only sometimes. I believe at one point we considered loosening this up a bit more but there were other issues.
A structure field or property name can be a reserved word or even sometimes an invalid property name if created externally (java,com or .net) or using mystruct.('propname'), syntax. For a class property this class has two properties. Note that reserved names can be used if not declared in the properties block:
classdef tstclass <dynamicprops
properties
prop1
end
methods
function obj=tstclass
addprop(obj,'global')
end
end
end
try :
tc=tstclass
tc.global=4
tc.addprop('propertiess')
tc.properties=5

  0 件のコメント

サインイン to comment.


Giles
回答者: Giles
2018 年 10 月 26 日

Thanks. However, using dynamic properties would only add the property to a given instance of the class, not to the class a whole, correct?
If it's only parsing the classdef script that's the issue and nothing else, you might consider amending the parser so that it can understand a keyword property name in the properties block? (In my case I really could use a property called "events"--turns out there aren't a lot of good synonyms for that word in the English language!)

  0 件のコメント

サインイン to comment.



Translated by