octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can someone test this for me in MATLAB?


From: Michael D. Godfrey
Subject: Re: Can someone test this for me in MATLAB?
Date: Fri, 04 Jan 2013 11:18:27 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0

On 01/04/2013 10:33 AM, Michael Goffioul wrote:
On Thu, Jan 3, 2013 at 9:31 PM, Ben Abbott <address@hidden> wrote:
On Jan 3, 2013, at 9:22 PM, Michael Goffioul wrote:

> On Thu, Jan 3, 2013 at 9:11 PM, Ben Abbott <address@hidden> wrote:
> On Jan 3, 2013, at 8:05 PM, Michael Goffioul wrote:
>
> > Hi,
> >
> > Could someone test something I'm wondering about classdef reload in MATLAB. Let's say there's a class defined as:
> >
> > classdef ClassA < handle
> >   properties
> >     p1 = 0;
> >   end
> > end
> >
> > At the prompt, create an object of ClassA:
> >
> > x = ClassA()
> >
> > Then, without closing MATLAB, edit the classdef file and a second property, the class now looks like this:
> >
> > classdef ClassA < handle
> >   properties
> >     p1 = 0;
> >     p2 = 1;
> >   end
> > end
> >
> > At the prompt, create a second object of ClassA:
> >
> > y = ClassA()
> >
> > The question is: what are the properties of x and y?
> >
> > A similar question occurs for methods. If the initial definition of ClassA contains a method m1. After creation of x, a second method m2 is added and another object is created. Is method m2 valid for object x? That is, does this succeeds (assuming m2 does not take any argument):
> >
> > x.m2()
> >
> > Michael.
>
> (I used the wrong email account the first time, so I'm resending)
>
> My first impression was that the objects were registered with the class ... but they apparently are not(?)
>
> "clear x y" did not allow the modified class to be used.  I had to "clear all" before the modified class could be used.   I assume that implies that a class cannot be over-ridden once is has been loaded, unless it is cleared first.
>
> In any event, the command you asked for and some more are below.
>
> x = ClassA ()
> x =
>
>  ClassA handle
>
>  Properties:
>    p1: 0
>
>  Methods, Events, Superclasses
>
> y = ClassA ()
> Warning: The class file for 'ClassA' has been changed, but the change cannot be applied because objects based on the old class file still exist. If you use those
> objects, you might get unexpected results. You can use the 'clear' command to remove those objects. See 'help clear' for information on how to remove those
> objects.
>
> y =
>
>  ClassA handle
>
>  Properties:
>    p1: 0
>
>  Methods, Events, Superclasses
>
> clear y
> x = ClassA
> Warning: The class file for 'ClassA' has been changed, but the change cannot be applied because objects based on the old class file still exist. If you use those
> objects, you might get unexpected results. You can use the 'clear' command to remove those objects. See 'help clear' for information on how to remove those
> objects.
>
> x =
>
>  ClassA handle
>
>  Properties:
>    p1: 0
>
>  Methods, Events, Superclasses
>
> clear x y
> x = ClassA
> Warning: The class file for 'ClassA' has been changed, but the change cannot be applied because objects based on the old class file still exist. If you use those
> objects, you might get unexpected results. You can use the 'clear' command to remove those objects. See 'help clear' for information on how to remove those
> objects.
>
> x =
>
>  ClassA handle
>
>  Properties:
>    p1: 0
>
>  Methods, Events, Superclasses
>
> >> clear all
> >> x = ClassA
>
> x =
>
>  ClassA handle
>
>  Properties:
>    p1: 0
>    p2: 0
>
>  Methods, Events, Superclasses
>
> Ben
>
> Interesting, thanks. I suppose the result is the same with methods? And does it matter if ClassA does not inherit from handle?
>
> Also does MATLAB reload methods that are defined in separate files? To test this, you need to define the following files, using a @-folder:
>
> @ClassA/ClassA.m
> @ClassA/m1.m
>
> with the respective content:
>
> @ClassA/ClassA.m:
>
> classdef ClassA < handle
>   methods
>     result = m1 (obj)
>   end
> end
>
> @ClassA/m1.m:
>
> function result = m1 (obj)
>   result = 1;
> end
>
I did this and got:
>> x = ClassA    

x =

  ClassA handle with no properties.

>>

> I'm also wondering how MATLAB behaves with class inheritance and file modifications. Let's say you have the following classes (in separate files):
>
> classdef ClassA < handle
>   methods
>     function out = m1 (obj)
>       out = 1;
>     end
>   end
> end
>
> classdef ClassB < ClassA
>   methods
>     function out = m1 (obj)
>       out = 0;
>     end
>   end
> end
>
> Then at MATLAB prompt, you type: ?ClassB
>
Got:
>> ?ClassB

ans =

  0x0 empty meta.class handle
  Package: meta

  Properties:
    Name
    Description
    DetailedDescription
    Hidden
    Sealed
    ConstructOnLoad
    InferiorClasses
    Properties
    Methods
    Events
    SuperClasses
    ContainingPackage


> Then you modify ClassA and tag m1 as Sealed, that is:
>
> classdef ClassA < handle
>   methods (Sealed)
>     function out = m1 (obj)
>       out = 1;
>     end
>   end
> end
>
> Normally, MATLAB should complain when initializing ClassB as it tries to override a sealed method. But I don't know when it complains: when loading the class only (that is: ?ClassB), or when using the class the first time (that is: creating a ClassB object).
>
Got:
>> ?ClassB

ans =

  0x0 empty meta.class handle
  Package: meta

  Properties:
    Name
    Description
    DetailedDescription
    Hidden
    Sealed
    ConstructOnLoad
    InferiorClasses
    Properties
    Methods
    Events
    SuperClasses
    ContainingPackage


>> ?ClassA

ans =

  meta.class handle
  Package: meta

  Properties:
                   Name: 'ClassA'
            Description: ''
    DetailedDescription: ''
                 Hidden: 0
                 Sealed: 0
        ConstructOnLoad: 0
        InferiorClasses: {0x1 cell}
             Properties: {0x1 cell}
                Methods: {18x1 cell}
                 Events: {[1x1 meta.event]}
           SuperClasses: {[1x1 meta.class]}
      ContainingPackage: {}


>>

> Michael.

Michael,

I've got a full few days and need to catch on my sleep now ... If no one gets to this by Monday, I should have some time to take a look.

No problem, thanks.

Can anyone else help here?

I'd also like to know what happens when the class definition file changes due to path change:
1) have a class definition in the path; create an object of that class; change path such that the class definition file is not accessible anymore; is the object still valid? is it possible to create more objects of the class?
2) a variant of 1) is to have a superclass disappearing from the path, while the subclass is still available
3) a variant of 1) is when another class definition with the same name appears in the path: let's say you have 2 different class definitions d1/ClassA.m and d2/ClassA.m (with different methods and/or properties), then "addpath d1; x = ClassA(); rmpath d1; addpath d2; y = ClassA();";
4) a variant of 3) is to have a different superclass due to path change, while the subclass stays the same: you have ClassB.m, d1/ClassA.m and d2/ClassA.m, with ClassB inheriting from ClassA; then "addpath d1; x = ClassB(); rmpath d1; addpath d2; y = ClassB();"
5) a variant of 4) is without creating objects, simply accessing the metaclass with "?ClassB"

Michael.

I will try to pick up where Ben left off.  See above.
I will also try some of the items which follow your:
Can anyone else help here?

I'd also like to know what happens when the class definition file changes due to path change:

The version of Matlab that I have is : 7.9.0.529 (R2009b)

Tell me if this is too old.

MG





reply via email to

[Prev in Thread] Current Thread [Next in Thread]