开发者

Can a constructor access the name of the variable its object is being store to?

开发者 https://www.devze.com 2023-04-05 00:28 出处:网络
I want the following code to set the \'name\' field of the myClass instance to be equal to \'super_awesome_name\':

I want the following code to set the 'name' field of the myClass instance to be equal to 'super_awesome_name':

super_awesome_na开发者_C百科me = myClass();

where the myClass constructor is as follows:

function obj = myClass()
    obj.name = ???;
end

Is there some way to do this in Matlab? Right now I can accomplish something very similar with assignin, but it makes the syntax very unnatural:

 myClass(super_awesome_name);

where the myClass constructor is as follows:

function obj = myClass(var_name)
    obj.name = assignin('caller',var_name, obj);
end


As far as I know, there is no convenient way of doing so. You can see this yourself since MATLAB will have to perform some when constructing an object like that.

  1. First construct the object
  2. Store that object in super_awesome_name

But step 1 needs information from step 2 (i.e. the name of the variable). So you will have to work around that.

You solution may work, but it leaves a lot of room for error. Just consider the case when super_awesome_name is undefined before the construction: this will throw an exception due to an unitialized variable.

IMHO it is easier to make a constructor that accepts a string containing the name (or possibly a variable, as you can get the variable name of an argument by using inputname). Using inputname, however is just a bit clearer than using assignin.

Why do you need such functionality? I'm thinking of a situation where you might do something like:

x = myObjectThatHasAName(); % x.name contains 'x'
y = myObjectThatHasAName(); % y.name contains 'y'
x = y;                      % x.name contains 'y' !

So that might be the behavior you are looking for, but it may become very confusing if you pass the variables along a lot. It looks to me like you are mixing debugging information into your actual computations.

If you need the functionality to determine the actual variable name in another method or function, just use inputname. Then you won't need to keep track of the variable name yourself with all confusing consequences. That would be what I'd propose to do in most cases. You can see an example in the MATLAB documentation.

On the other hand, I think the clearest way to store the variable name inside the object, could be something like:

classdef myObjectThatHasAName
   properties
      name = '';
   end
   methods
      function obj = myObjectThatHasAName()
      end
      function obj = storeName(obj,name)
         if nargin < 2
            name = inputname(1);
         end
         if ~isempty(name) % && isempty(obj.name)
            obj.name = inputname(1);
         end
      end
   end
end

I haven't tested this (lack of MATLAB on my current computer), but the following code should do it:

x = myObjectThatHasAName();
x = x.storeName(); 
% or x = storeName(x);
% or x = storeName(x,'x');

If you don't mind the overhead, you could always incorporate the storeName into each other method (and forget about calling storeMethod outside of the methods). This keeps track of the variable name where the object resided before the last assignment. A possible method might look like this:

function obj = otherMethod(obj,arguments)
   obj = obj.storeName(inputname(1));
   % actual code for the method
end

That way the name is set when you first call any method on the object (this almost does what you want, but I guess it will do what you need in most cases). You can of course adjust the code to just remember the first assignment (just comment out the code in storeName).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号