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: Ben Abbott
Subject: Re: Can someone test this for me in MATLAB?
Date: Thu, 03 Jan 2013 21:31:20 -0500

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'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
> 
> 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).
> 
> 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. 

Ben



reply via email to

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