Thread Subject: Nested functions and oop

Subject: Nested functions and oop

From: Philippe Maincon

Date: 24 Feb, 2006 03:18:06

Message: 1 of 10

Having used Matlab's object oriented programming for a while, I am
quite confused by the recent introduction of nested functions and
their combination with function handles.

The following code shows how nested functions, structures and
handles are used to create... a new syntax for oop in Mat lab.

function obj = MyClass(input)
x = input;
y = 3.14159;
% obj = ParentClass(input/2);
obj.increment = @increment;
obj.set = @set;
obj.display = @display;
    function increment
        x = x+1;
    end
    function set(in)
        y = in;
    end
    function display
        x
        y
    end
end

Using the class remind furiously of C++
c = MyClass(27);
c.display()
c.increment()
c.set(2.71)
c.display()
...

This gives MATLAB the questionable privilege of being the 1st
programming language with 2 competing oop syntaxes! Using this
syntax, one looses quite a few MATLAB features, including, as far as
I can see, the ability to overload operators, MATLABS's automated
calls to "display(o)" (it won't call o.display(), for sure). It seems
to me that anything that can be done with nested funs and handles can
be done with standard matlab objects, and that we have a case of
language cluttering.

So my questions to all wise guys:
- What did I miss? What can be done with nested funs that can't be
done with objects? Maybe there are efficiency issues? Or was it a
bid to make basic oop functionality available to users that shy
away/are unfamiliar with oop?

- Was it MathWorks intention to provide an alternative oop syntax, or
am I victim of my own creativity? Is this related to MathWorks call
for beta users for some new form of oop, a few months ago?

- Can we rely on the present oop syntax to be maintained in the
future? What are the plans?

I look forward to your comments

Philippe

P.S. To get in touch, check out www.safran.co.za. Sorry for the
hassle, but I do not enjoy spam!

Subject: Nested functions and oop

From: David Mackenzie

Date: 24 Feb, 2006 01:07:44

Message: 2 of 10


Philippe Maincon schrieb:

<snip> wonderfully clear insight... <snip>

Philippe,
although I can't answer your questions I'd like to thank you for
posting this - I haven't been very active in MATLAB recently and it
wasn't at all clear to me from the descriptions of nested functions
floating around that this is possible. A couple of years ago I
struggled bravely with MATLAB objects (in an attempt to build a graphic
user object toolbox, a central aim of which was to implement something
similar to what in Version 7 appeared as uipanel objects) but wasn't
really happy with the results, the main problem being that assignment
of the object was necessary if attributes were to be updated - very
ugly. In order to avoid using global variables I had to use existing
graphic objects to store attributes. In contrast, your solution allows
proper instantiation of objects!

For example, if you change the increment function to allow a couple of
dummy parameters:
    function increment(obj,event)
and define a pushbutton from the command line:
    uicontrol('style','pushbutton','callback',c.increment)
this works fine when you click the button. However, I never found a
good way to do this with MATLAB objects without using global variables
(admittedly I wasn't familiar with function handles at that time).

Maybe someone else knew this already, but I've never seen it explained.
 Somehow this important feature wasn't highlighted in the release
notes. I think your insight is certainly worthy of the File Exchange.

Best regards,
David Mackenzie

Subject: Nested functions and oop

From: Michael Wild

Date: 24 Feb, 2006 10:57:12

Message: 3 of 10

using function handles in this way seems to be a very convenient idea.
and i don't think that it is cluttering the language, since this
methodology doesn't really define methods, but rather registers them as
data fields which happen to be function handles and thus are executable.
it is more similar to registering callbacks in some gui-toolkit, or more
general, in some well-known design-patterns.

as these function handles are not really methods, it is clear that they
won't be called by matlab (how should it know about them being actually
methods?).

what comes to mind, is using the conventional way of oop in matlab, but
then register the methods in the way you showed. this enables you to get
both: overloading and this convenient way of calling the callback.
however, i'm not sure what this means with respect to polymorphism (does
matlab support that?). it will probably fail utterly...

to be honest, i rarely ever use the oop feature offered by matlab. i
just think it is too clumsy. tmw really should think about redoing the
whole thing. for one: define a whole class in one file. and then
handling member access is another thing which makes oop in matlab a real
pain. it is just plain, that the oo feature whas plugged on top of a
procedural language.


michael

Subject: Nested functions and oop

From: Brad Phelan

Date: 24 Feb, 2006 05:05:33

Message: 4 of 10

David Mackenzie wrote:
> Philippe Maincon schrieb:
>
> <snip> wonderfully clear insight... <snip>
>
> Philippe,
> although I can't answer your questions I'd like to thank you for
> posting this - I haven't been very active in MATLAB recently and it
> wasn't at all clear to me from the descriptions of nested functions
> floating around that this is possible. A couple of years ago I
> struggled bravely with MATLAB objects (in an attempt to build a graphic
> user object toolbox, a central aim of which was to implement something
> similar to what in Version 7 appeared as uipanel objects) but wasn't
> really happy with the results, the main problem being that assignment
> of the object was necessary if attributes were to be updated - very
> ugly. In order to avoid using global variables I had to use existing
> graphic objects to store attributes. In contrast, your solution allows
> proper instantiation of objects!
>
> For example, if you change the increment function to allow a couple of
> dummy parameters:
> function increment(obj,event)
> and define a pushbutton from the command line:
> uicontrol('style','pushbutton','callback',c.increment)
> this works fine when you click the button. However, I never found a
> good way to do this with MATLAB objects without using global variables
> (admittedly I wasn't familiar with function handles at that time).
>
> Maybe someone else knew this already, but I've never seen it explained.
> Somehow this important feature wasn't highlighted in the release
> notes. I think your insight is certainly worthy of the File Exchange.
>
> Best regards,
> David Mackenzie
>

I've been using the above techniques for object oriented programming in
Matlab for a while now. See the below snippets on my website for some
tutorials.

http://xtargets.com/snippets/posts/show/39
http://xtargets.com/snippets/posts/show/42

Nested functions can be used to build object oriented frameworks but
that is merely a side effect. They are usefull in their own right. If
you want to get technical then yes objects and nested functions are
dual concepts.

An object is data with a set of nested functions attached.
A nested function is a function with some data attached.

Mathworks could build a proper OO framework around nested functions I
guess but I don't believe they will.

Brad

Subject: Nested functions and oop

From: Philippe Maincon

Date: 24 Feb, 2006 06:48:30

Message: 5 of 10

David, Michael, Brad,

Thank you for your answers, I definitely learned something
interesting today. I will try to summarise what I learned from your
contributions:

- A strong anti-bug feature of Matlab is that a function can only
modify a variable if the variable appears on the left hand side of
the function call. This is thanks to MATLAB's pass by value
[apparent] behaviour. This makes it difficult to have different part
of a software work on the same database (in C++ and the like, objects
would have received a POINTER to the database, and used it to access
the data).

Wether one makes them object-like or not, nest&handle allows to pass
to a function THE RIGHT TO DO A SPECIFIC OPERATION on a set of data.
This breaks the above anti-bug rule, but in a very elegant manner:
IfUserWants(a.makeItGreen);
The function IfUserWants can modify the data in "a" but only in one
way: "makeItGreen" It can't make it red or erase it...

A significant advantage over pointers is that "the last one out
switches off the light", or at least, I strongly suspect so. Once the
function handles are generated they "point" to the data, and you can
copy these handles (this acts as "shallow copy"). In C++ et al., if
you delete all your pointers, you have a memory leak. Here... you can
distribute "pointers" around, and yet do not need to bother about a
destructor.

- One can "hack" nested functiosn & handles to create hack-object,
but note the unusual (hence risky) features:
a = b is a shallow copy for hack-objects, and a deep copy for
standard objects... For hack-objects, function argument passage is by
reference, for standard objects, it's by value. If you pass a
hack-object to a function, you basicaly pass the rights to do all
permissible operations.

I certainly retract the comment that nest&handle provides nothing
new. As for hack-objects, I reckon a) there is a market for them b)
be very, very cautious!

Regards

Philippe

Subject: Nested functions and oop

From: Peter Boettcher

Date: 24 Feb, 2006 09:57:39

Message: 6 of 10

"Philippe Maincon" <bogus@see.below.co.za> writes:

> This gives MATLAB the questionable privilege of being the 1st
> programming language with 2 competing oop syntaxes! Using this
> syntax, one looses quite a few MATLAB features, including, as far as
> I can see, the ability to overload operators, MATLABS's automated
> calls to "display(o)" (it won't call o.display(), for sure).

I haven't really tried it myself, but take a look at the code for
the memmapfile class in a recent MATLAB. It seems to be using a new
class syntax that uses the nested function tricks you describe, but
also has real overloaded display routines and such.


--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: Nested functions and oop

From: Brad Phelan

Date: 24 Feb, 2006 12:35:40

Message: 7 of 10

Peter Boettcher wrote:
> "Philippe Maincon" <bogus@see.below.co.za> writes:
>
>> This gives MATLAB the questionable privilege of being the 1st
>> programming language with 2 competing oop syntaxes! Using this
>> syntax, one looses quite a few MATLAB features, including, as far as
>> I can see, the ability to overload operators, MATLABS's automated
>> calls to "display(o)" (it won't call o.display(), for sure).
>
> I haven't really tried it myself, but take a look at the code for
> the memmapfile class in a recent MATLAB. It seems to be using a new
> class syntax that uses the nested function tricks you describe, but
> also has real overloaded display routines and such.
>
>

I think it was an accident for that file to not be p-coded. It looks
like a new unsupported API. I wouldn't garuntee that the API will stay
the same in future releases. It looks interesting though.

B

--
http://xtargets.com

Subject: Nested functions and oop

From: Philippe Maincon

Date: 26 Feb, 2006 03:32:10

Message: 8 of 10

Peter,

That was an interesting peep in the code, and answers more of my
inital questions about the future of oo in MATLAB. I bet this is the
syntax that was released for beta testing last year. (I was on a
dial-up line in the bush in Germany and couldn't join the feast)

Mike's call for "all methods in one file" is about to be heeded -
except for the comments that have to be in their own file (hey
MathWorks, if you start a job, do it completely! Put comments into
the main file, and reprogram "help" - OK' I'm sure you plan to do
this actually)

And there is a destructor now, of course, necessary for memmapfile to
properly close down a file access. But lack of call to destructor
does not seem to imply memory leak. You know what, I have some deep
respect those guys.

Good news of the day: this looks backward compatible with present day
syntax (well anything else would have been a serious surprise). I
don't need to send my 10.000 lines of code to the bin... Dear
MathWorks, with the next release, give us a utility that packs old
method-files into one new-syntax file, pretty please.

Cheers

Philippe

Subject: Nested functions and oop

From: David Mackenzie

Date: 1 Mar, 2006 00:14:17

Message: 9 of 10

If anyone from The Mathworks is following this thread - I'd love to see
a white paper on this subject. There's obviously much more already
implemented in MATLAB than is currently documented.

David

Subject: Nested functions and oop

From: Raymond Norris

Date: 23 Mar, 2006 12:31:22

Message: 10 of 10

Brad-

A very large part of OOP is inheritance. Without a true class
object, how do you subclass your data structures?

Raymond

Brad Phelan wrote:
>
>
> David Mackenzie wrote:
>> Philippe Maincon schrieb:
>>
>> <snip> wonderfully clear insight... <snip>
>>
>> Philippe,
>> although I can't answer your questions I'd like to thank you
for
>> posting this - I haven't been very active in MATLAB recently
and
> it
>> wasn't at all clear to me from the descriptions of nested
> functions
>> floating around that this is possible. A couple of years ago I
>> struggled bravely with MATLAB objects (in an attempt to build a
> graphic
>> user object toolbox, a central aim of which was to implement
> something
>> similar to what in Version 7 appeared as uipanel objects) but
> wasn't
>> really happy with the results, the main problem being that
> assignment
>> of the object was necessary if attributes were to be updated -
> very
>> ugly. In order to avoid using global variables I had to use
> existing
>> graphic objects to store attributes. In contrast, your
solution
> allows
>> proper instantiation of objects!
>>
>> For example, if you change the increment function to allow a
> couple of
>> dummy parameters:
>> function increment(obj,event)
>> and define a pushbutton from the command line:
>> uicontrol('style','pushbutton','callback',c.increment)
>> this works fine when you click the button. However, I never
> found a
>> good way to do this with MATLAB objects without using global
> variables
>> (admittedly I wasn't familiar with function handles at that
> time).
>>
>> Maybe someone else knew this already, but I've never seen it
> explained.
>> Somehow this important feature wasn't highlighted in the
release
>> notes. I think your insight is certainly worthy of the File
> Exchange.
>>
>> Best regards,
>> David Mackenzie
>>
>
> I've been using the above techniques for object oriented
> programming in
> Matlab for a while now. See the below snippets on my website for
> some
> tutorials.
>
> <http://xtargets.com/snippets/posts/show/39>
> <http://xtargets.com/snippets/posts/show/42>
>
> Nested functions can be used to build object oriented frameworks
> but
> that is merely a side effect. They are usefull in their own right.
> If
> you want to get technical then yes objects and nested functions are
> dual concepts.
>
> An object is data with a set of nested functions attached.
> A nested function is a function with some data attached.
>
> Mathworks could build a proper OO framework around nested functions
> I
> guess but I don't believe they will.
>
> Brad
>

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com