<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: A Way to Create Reusable Tools</title>
	<atom:link href="http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Fri, 17 Feb 2012 13:41:33 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Dimitri Shvorob</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-24422</link>
		<dc:creator>Dimitri Shvorob</dc:creator>
		<pubDate>Sun, 06 Jan 2008 14:28:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-24422</guid>
		<description>My apologies: here&#039;s the correct link.

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=18223&amp;objectType=FILE</description>
		<content:encoded><![CDATA[<p>My apologies: here&#8217;s the correct link.</p>
<p><a href="http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=18223&#038;objectType=FILE" rel="nofollow">http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=18223&#038;objectType=FILE</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dimitri Shvorob</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-24421</link>
		<dc:creator>Dimitri Shvorob</dc:creator>
		<pubDate>Sun, 06 Jan 2008 14:27:34 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-24421</guid>
		<description>With due appreciation for Loren&#039;s Matlab prowess, it&#039;s the comments by Michael Corvin that are the real revelation in this thread. So, in effect, Matlab has a second, &#039;backdoor&#039; way of creating objects? Set up a function

function obj = myQuasiObject
..

with some nested functions

function nestedFunction1
..

function nestedFunction2
..

then create structure &#039;obj&#039;, and add to it

(a) arbitrary values (these are your class properties),
(b) handles to the nested functions (these are your class methods).

Voila! Class myQuasiObject is ready to instantiate

obj = myQuasiObject;

and manipulate - with the customary &#039;dot&#039; syntax not available with Matlab&#039;s &#039;frontdoor&#039; objects.

obj.nestedFunction1

Not being able to have private properties and methods is a nuisance, but inheritance seems to be alive and well: why not invoke one &#039;object function&#039; from within another?

Is this added functionality a good thing? I think that Loren&#039;s code - just touching on the matter, really - gained but a tiny bit of syntactic sugar from not using a &#039;normal&#039; class, with a transparent constructor and methods. On the other hand, consider this FEX submission, a purposeful play on persistence and scope implications of the &#039;backdoor objects&#039; approach

http://www.mathworks.com/matlabcentral/fileexchange/saveRating.do

for an example of how one can create a debugging nightmare in less than 20 lines.</description>
		<content:encoded><![CDATA[<p>With due appreciation for Loren&#8217;s Matlab prowess, it&#8217;s the comments by Michael Corvin that are the real revelation in this thread. So, in effect, Matlab has a second, &#8216;backdoor&#8217; way of creating objects? Set up a function</p>
<p>function obj = myQuasiObject<br />
..</p>
<p>with some nested functions</p>
<p>function nestedFunction1<br />
..</p>
<p>function nestedFunction2<br />
..</p>
<p>then create structure &#8216;obj&#8217;, and add to it</p>
<p>(a) arbitrary values (these are your class properties),<br />
(b) handles to the nested functions (these are your class methods).</p>
<p>Voila! Class myQuasiObject is ready to instantiate</p>
<p>obj = myQuasiObject;</p>
<p>and manipulate &#8211; with the customary &#8216;dot&#8217; syntax not available with Matlab&#8217;s &#8216;frontdoor&#8217; objects.</p>
<p>obj.nestedFunction1</p>
<p>Not being able to have private properties and methods is a nuisance, but inheritance seems to be alive and well: why not invoke one &#8216;object function&#8217; from within another?</p>
<p>Is this added functionality a good thing? I think that Loren&#8217;s code &#8211; just touching on the matter, really &#8211; gained but a tiny bit of syntactic sugar from not using a &#8216;normal&#8217; class, with a transparent constructor and methods. On the other hand, consider this FEX submission, a purposeful play on persistence and scope implications of the &#8216;backdoor objects&#8217; approach</p>
<p><a href="http://www.mathworks.com/matlabcentral/fileexchange/saveRating.do" rel="nofollow">http://www.mathworks.com/matlabcentral/fileexchange/saveRating.do</a></p>
<p>for an example of how one can create a debugging nightmare in less than 20 lines.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-24321</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 04 Jan 2008 14:44:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-24321</guid>
		<description>Vijay-

When the function handles are constructed in the same environment (same function, workspace and during the same invocation), they DO all share the same workspace - no extra copies.  That&#039;s how they can do the near magic that they can.

--Loren</description>
		<content:encoded><![CDATA[<p>Vijay-</p>
<p>When the function handles are constructed in the same environment (same function, workspace and during the same invocation), they DO all share the same workspace &#8211; no extra copies.  That&#8217;s how they can do the near magic that they can.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vijay Iyer</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-24320</link>
		<dc:creator>Vijay Iyer</dc:creator>
		<pubDate>Fri, 04 Jan 2008 14:32:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-24320</guid>
		<description>I&#039;m quite intrigued by the idea of using nested functions to do OOP in Matlab.

The one thing I wonder about is memory usage. I don&#039;t really understand what gets stored into a function handle. I know that a function handle &quot;contains&quot; all the variables in its scope, but I&#039;m not exactly sure how. If you create an object in the way described here, consisting of a struct with numerous function handles, I&#039;m assuming that each handle effectively &quot;points&quot; to the same shared workspace of variables? this would be quite cheap and acceptable, even if the data in that workspace became quite large, since there would only be one copy. 

I became concerned that the reality may be more memory-wasteful, when looking at the documentation for function handles and nested functions, which says
&quot;What in fact happens though is that MATLAB stores variables such as VExt inside the function handle itself when it is being constructed.&quot; (see the documentation for nested functions).

This can&#039;t be correct, right? this would lead to the situation where each function handle had its own workspace.

But on the other hand, I find it somewhat magical that each of the function handles ends up pointing to the same workspace (what I want/hope to happen). It seems then that as you create a function handle, the interpreter must check to see if a workspace with the same variables in scope exists, in which case it doesn&#039;t duplicate the workspace, but rather &quot;points&quot; to the first one. Is this what happens?</description>
		<content:encoded><![CDATA[<p>I&#8217;m quite intrigued by the idea of using nested functions to do OOP in Matlab.</p>
<p>The one thing I wonder about is memory usage. I don&#8217;t really understand what gets stored into a function handle. I know that a function handle &#8220;contains&#8221; all the variables in its scope, but I&#8217;m not exactly sure how. If you create an object in the way described here, consisting of a struct with numerous function handles, I&#8217;m assuming that each handle effectively &#8220;points&#8221; to the same shared workspace of variables? this would be quite cheap and acceptable, even if the data in that workspace became quite large, since there would only be one copy. </p>
<p>I became concerned that the reality may be more memory-wasteful, when looking at the documentation for function handles and nested functions, which says<br />
&#8220;What in fact happens though is that MATLAB stores variables such as VExt inside the function handle itself when it is being constructed.&#8221; (see the documentation for nested functions).</p>
<p>This can&#8217;t be correct, right? this would lead to the situation where each function handle had its own workspace.</p>
<p>But on the other hand, I find it somewhat magical that each of the function handles ends up pointing to the same workspace (what I want/hope to happen). It seems then that as you create a function handle, the interpreter must check to see if a workspace with the same variables in scope exists, in which case it doesn&#8217;t duplicate the workspace, but rather &#8220;points&#8221; to the first one. Is this what happens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: G.A.M.</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-18690</link>
		<dc:creator>G.A.M.</dc:creator>
		<pubDate>Fri, 05 Oct 2007 23:59:08 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-18690</guid>
		<description>Michael Corvin said:
&gt;I’ve implemented several variants of OO frameworks based on
&gt; closures that add inheritance.

Michael, are you willing to share your OO framework? I would love to be able to use something like this in Matlab.</description>
		<content:encoded><![CDATA[<p>Michael Corvin said:<br />
&gt;I’ve implemented several variants of OO frameworks based on<br />
&gt; closures that add inheritance.</p>
<p>Michael, are you willing to share your OO framework? I would love to be able to use something like this in Matlab.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mathias Ortner</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-16394</link>
		<dc:creator>Mathias Ortner</dc:creator>
		<pubDate>Tue, 21 Aug 2007 07:06:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-16394</guid>
		<description>Thanks Michael for this insight. 

Dear Loren,

I regularly use the nested function trick for numerical computation. It makes life so much easier... However I find the usually fantastic help tool a little weak on this side.

I therefore had to write a tool for that (hyperSubFunHelp on MCFE). But I think an easy to use tool for displaying help about subfunctions is definitively lacking (help function&gt;subfunction is not really easy to use).

Regards, 

Mathias</description>
		<content:encoded><![CDATA[<p>Thanks Michael for this insight. </p>
<p>Dear Loren,</p>
<p>I regularly use the nested function trick for numerical computation. It makes life so much easier&#8230; However I find the usually fantastic help tool a little weak on this side.</p>
<p>I therefore had to write a tool for that (hyperSubFunHelp on MCFE). But I think an easy to use tool for displaying help about subfunctions is definitively lacking (help function&gt;subfunction is not really easy to use).</p>
<p>Regards, </p>
<p>Mathias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Corvin</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-16384</link>
		<dc:creator>Michael Corvin</dc:creator>
		<pubDate>Fri, 17 Aug 2007 04:28:57 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-16384</guid>
		<description>Wobbly, as I tried to convey, above, it&#039;s to achieve encapsulation of the object state and to make the object pass by reference (function handles are the ONLY thing that is pass by reference in Matlab, everything else is pass by value [ok, ok, libpointers and javaArray are too, but that&#039;s arcane juju for most folks...]). NB, the objects created with the Matlab OOPS are pass by value...

Ok, what does that mean...

In practical terms, it realizes what is shown in the second block of code, above.  I.e., if a is an object then b = a refers to the *same* object, not a copy. If each field on the struct representing an object is a function handle, all copies of that struct have the same function handle values and they all point to the same thing.

Here&#039;s a practical example of how direct access breaks this:

Modify the example, above, by adding a field, age, directly to the class structure:


function obj = foo

name = &#039;&#039;;  
obj.age = [];

obj.name = @accessName;
  function out = accessName(in)
    if nargin, name = in; end
    out = name;
  end

end


Now, create an instance (object) of the foo class and set its attributes:


&gt;&gt; a = foo
a = 
     age: []
    name: @foo/accessName
&gt;&gt; a.name(&#039;Arthur Dent&#039;); a.age = 42;
&gt;&gt; disp( a.name() ); disp( a.age )
Arthur Dent
    42


Copy it:


&gt;&gt; b = a;
&gt;&gt; disp( b.name() ); disp( b.age )
Arthur Dent
    42


Now modify the first one:


&gt;&gt; a.name(&#039;Ford Prefect&#039;); a.age = 612;
&gt;&gt; disp( a.name() ); disp( a.age )
Ford Prefect
   612


But the second one is broken since the age is still the value it had when you did b = a :


&gt;&gt; disp( b.name() ); disp( b.age )
Ford Prefect
    42


So, &quot;b&quot; is a partially-connected twin of &quot;a&quot; from the Island of Doctor Moreau... a misshapen creature that likely would cause heinous bugs in your code!

The way to think about stuff in the OO paradigm is as things that interact with each other purely by messages.  A message to an object asks it invoke an operation that transforms its state, returns a value (which, purely speaking, itself would be an object...), or does both. You don&#039;t want the outside world to go messin&#039; with an object&#039;s state directly, just via the set of messages it can respond to.  The set of valid messages defines the interface that the object presents to the world.  How it does things, behind the curtain, is hidden.  That&#039;s what one means by encapsulation. (Ok, there&#039;s more to it... see the Encyclopedia Galatica, or, wikipedia as a starting point for more...)  It enables all sorts of crunchy goodness - good modularity - when applied properly.

Usually (but not always in all languages...), a message consists of the method name and its arguments. E.g. in the syntax used here the method invocation a.name(&#039;Bart&#039;) is sending the message &#039;name&#039; with the argument &#039;Bart&#039; to the object referenced by &#039;a&#039;.

In OO hackin&#039;, when you&#039;re creating a bunch of objects to do something useful, you usually want to have objects each have references to several of the other objects so that they can interact with each other.

E.g., A bunch of person objects (a collection of person objects)named &quot;dude&quot;,&quot;biker&quot; and &quot;hacker&quot; all walk into a room object.  The room object already contains a collection of person objects, &quot;boring guy&quot;, &quot;Alan Kay&quot; and &quot;hot babe&quot;. The new guys get added to the room&#039;s collection of persons. Each one of the new person objects wants to iterate over the collection of persons in the room (skipping themselves, unless they have multiple personalities...) to execute the &quot;scope_em_out&quot; method on each person in the room - i.e., each new guy wants a list of references to each person in the room collection.  Then they can choose their next message, if any, to send to one or more of the other persons (&quot;dude&quot; and &quot;biker&quot; both send &quot;hi_howyadoin?&quot; messages to the &quot;hot babe&quot; and to each other while &quot;hacker&quot; is totally into a &quot;talk_with_guru&quot; message to &quot;Alan Kay&quot;...).

BTW, Alan Kay and the rest of the brilliant gang at Xerox Parc were the inventors of Smalltalk, a &#039;pure&#039; object language, starting around &#039;69.  There&#039;s a fun, open source version still around and kickin&#039;, called Squeak, and some commercial versions that a few companies still quietly use to get a lot done.  Smalltalk is one of the few imperative languages even more fluid to hack than Matlab since one can actually change code *while it&#039;s executing*.  A good portion of the Squeak community are educators who use it to get kids (even elementary school) into programming in a really fun environment.

That was probaby waaaaaay more than you wanted, wobbly!...

Happy Matlabing!
Michael</description>
		<content:encoded><![CDATA[<p>Wobbly, as I tried to convey, above, it&#8217;s to achieve encapsulation of the object state and to make the object pass by reference (function handles are the ONLY thing that is pass by reference in Matlab, everything else is pass by value [ok, ok, libpointers and javaArray are too, but that's arcane juju for most folks...]). NB, the objects created with the Matlab OOPS are pass by value&#8230;</p>
<p>Ok, what does that mean&#8230;</p>
<p>In practical terms, it realizes what is shown in the second block of code, above.  I.e., if a is an object then b = a refers to the *same* object, not a copy. If each field on the struct representing an object is a function handle, all copies of that struct have the same function handle values and they all point to the same thing.</p>
<p>Here&#8217;s a practical example of how direct access breaks this:</p>
<p>Modify the example, above, by adding a field, age, directly to the class structure:</p>
<p>function obj = foo</p>
<p>name = &#8221;;<br />
obj.age = [];</p>
<p>obj.name = @accessName;<br />
  function out = accessName(in)<br />
    if nargin, name = in; end<br />
    out = name;<br />
  end</p>
<p>end</p>
<p>Now, create an instance (object) of the foo class and set its attributes:</p>
<p>&gt;&gt; a = foo<br />
a =<br />
     age: []<br />
    name: @foo/accessName<br />
&gt;&gt; a.name(&#8216;Arthur Dent&#8217;); a.age = 42;<br />
&gt;&gt; disp( a.name() ); disp( a.age )<br />
Arthur Dent<br />
    42</p>
<p>Copy it:</p>
<p>&gt;&gt; b = a;<br />
&gt;&gt; disp( b.name() ); disp( b.age )<br />
Arthur Dent<br />
    42</p>
<p>Now modify the first one:</p>
<p>&gt;&gt; a.name(&#8216;Ford Prefect&#8217;); a.age = 612;<br />
&gt;&gt; disp( a.name() ); disp( a.age )<br />
Ford Prefect<br />
   612</p>
<p>But the second one is broken since the age is still the value it had when you did b = a :</p>
<p>&gt;&gt; disp( b.name() ); disp( b.age )<br />
Ford Prefect<br />
    42</p>
<p>So, &#8220;b&#8221; is a partially-connected twin of &#8220;a&#8221; from the Island of Doctor Moreau&#8230; a misshapen creature that likely would cause heinous bugs in your code!</p>
<p>The way to think about stuff in the OO paradigm is as things that interact with each other purely by messages.  A message to an object asks it invoke an operation that transforms its state, returns a value (which, purely speaking, itself would be an object&#8230;), or does both. You don&#8217;t want the outside world to go messin&#8217; with an object&#8217;s state directly, just via the set of messages it can respond to.  The set of valid messages defines the interface that the object presents to the world.  How it does things, behind the curtain, is hidden.  That&#8217;s what one means by encapsulation. (Ok, there&#8217;s more to it&#8230; see the Encyclopedia Galatica, or, wikipedia as a starting point for more&#8230;)  It enables all sorts of crunchy goodness &#8211; good modularity &#8211; when applied properly.</p>
<p>Usually (but not always in all languages&#8230;), a message consists of the method name and its arguments. E.g. in the syntax used here the method invocation a.name(&#8216;Bart&#8217;) is sending the message &#8216;name&#8217; with the argument &#8216;Bart&#8217; to the object referenced by &#8216;a&#8217;.</p>
<p>In OO hackin&#8217;, when you&#8217;re creating a bunch of objects to do something useful, you usually want to have objects each have references to several of the other objects so that they can interact with each other.</p>
<p>E.g., A bunch of person objects (a collection of person objects)named &#8220;dude&#8221;,&#8221;biker&#8221; and &#8220;hacker&#8221; all walk into a room object.  The room object already contains a collection of person objects, &#8220;boring guy&#8221;, &#8220;Alan Kay&#8221; and &#8220;hot babe&#8221;. The new guys get added to the room&#8217;s collection of persons. Each one of the new person objects wants to iterate over the collection of persons in the room (skipping themselves, unless they have multiple personalities&#8230;) to execute the &#8220;scope_em_out&#8221; method on each person in the room &#8211; i.e., each new guy wants a list of references to each person in the room collection.  Then they can choose their next message, if any, to send to one or more of the other persons (&#8220;dude&#8221; and &#8220;biker&#8221; both send &#8220;hi_howyadoin?&#8221; messages to the &#8220;hot babe&#8221; and to each other while &#8220;hacker&#8221; is totally into a &#8220;talk_with_guru&#8221; message to &#8220;Alan Kay&#8221;&#8230;).</p>
<p>BTW, Alan Kay and the rest of the brilliant gang at Xerox Parc were the inventors of Smalltalk, a &#8216;pure&#8217; object language, starting around &#8217;69.  There&#8217;s a fun, open source version still around and kickin&#8217;, called Squeak, and some commercial versions that a few companies still quietly use to get a lot done.  Smalltalk is one of the few imperative languages even more fluid to hack than Matlab since one can actually change code *while it&#8217;s executing*.  A good portion of the Squeak community are educators who use it to get kids (even elementary school) into programming in a really fun environment.</p>
<p>That was probaby waaaaaay more than you wanted, wobbly!&#8230;</p>
<p>Happy Matlabing!<br />
Michael</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wobbly</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-16375</link>
		<dc:creator>wobbly</dc:creator>
		<pubDate>Mon, 13 Aug 2007 03:48:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-16375</guid>
		<description>Micheal, you say: &quot;All access to internal state should be through accessor methods.&quot;
Could you explain what the actual reason is for this?
What is practically wrong with directly accessing simple data eg.  a.name=&#039;do less programming&#039;</description>
		<content:encoded><![CDATA[<p>Micheal, you say: &#8220;All access to internal state should be through accessor methods.&#8221;<br />
Could you explain what the actual reason is for this?<br />
What is practically wrong with directly accessing simple data eg.  a.name=&#8217;do less programming&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MLOOP</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-16373</link>
		<dc:creator>MLOOP</dc:creator>
		<pubDate>Fri, 10 Aug 2007 18:44:38 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-16373</guid>
		<description>Once matlab&#039;s mcos oops system is released building proper oops programs/classes will be much easier.  I will even say delightful. :)

I am begging to know:  1. if what we see in the matlab files will change much ie see memmapfile,  2. when will mcos be officially released.

The current oops system is very difficult to use if you want to have proper fully functional class.  And trying to do it with just nested functions isn&#039;t ideal either.


Please please oh please let us know when mcos will be available.</description>
		<content:encoded><![CDATA[<p>Once matlab&#8217;s mcos oops system is released building proper oops programs/classes will be much easier.  I will even say delightful. :)</p>
<p>I am begging to know:  1. if what we see in the matlab files will change much ie see memmapfile,  2. when will mcos be officially released.</p>
<p>The current oops system is very difficult to use if you want to have proper fully functional class.  And trying to do it with just nested functions isn&#8217;t ideal either.</p>
<p>Please please oh please let us know when mcos will be available.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Corvin</title>
		<link>http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-16367</link>
		<dc:creator>Michael Corvin</dc:creator>
		<pubDate>Fri, 10 Aug 2007 01:24:06 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/08/08/a-way-to-create-reusable-tools/#comment-16367</guid>
		<description>Good to see that the &quot;secret&quot; is getting out...!

Like Sean, I&#039;ve been using closures with nested functions and function handles to do &#039;lightweight&#039;/&#039;pseudo&#039; OO (lacking inheritance) since their introduction in 7.0.  I was inspired by similar patterns in Scheme. 

I also use anonymous functions quite a bit. Incidentally, by using anonymous functions (lambda functions...) one can hack many of the examples in the first chapters of the classic, &quot;Structure and Interpretation of Computer Programs&quot; (http://mitpress.mit.edu/sicp/).  This is a great way of getting a little taste of functional programming vs. the procedural, imperative paradigm that 99% of Matlabers probably are more used to.

A massive advantage to using closure objects can be that a function handle reference to an object (instance) method provides pass-by-reference, so, e.g., the same instance can be referenced in multiple collections.  This leads to the second massive advantage: persistence.  As long as there is a reference to the object its enclosed state exists and anyone who has a reference to it can use it.  There is no need to use globals ever again (DEATH to globals!), except, perhaps in exceptional circumstances.

In order to realize a closure object as a &#039;pure&#039; reference the structure returned by the main, enclosing function (effectively, the constructor) must be only a structure of function handles (or a single function handle pointing to a dispatcher).  Loren&#039;s example, volumeVisualization, breaks this a bit by putting some mutable state onto the structure, but that could be fixed easily.

All access to internal state should be through accessor methods.  As a hint, a convenient, minimalist accessor providing both get and set is shown by this example:

&lt;pre class=&quot;code&quot;&gt;
function obj = foo

% variables in the enclosing function are attributes
name = &#039;&#039;;  

% provide accessors for the ones you want to be &#039;public&#039;
obj.name = @accessName;  
  function out = accessName(in)
    if nargin, name = in; end
    out = name;
  end

% other methods change state (provide behavior)
...

end
&lt;/pre&gt;

Then....

&lt;pre class=&quot;code&quot;&gt;
a = foo;
a.name(&#039;I am a foo&#039;);
a.name() -&gt; &#039;I am a foo&#039;

b = a;
b.name() -&gt; &#039;I am a foo&#039;   % reference to the same object
&lt;/pre&gt;

I&#039;ve implemented several variants of OO frameworks based on closures that add inheritance.  Even the somewhat bulky, first forms (they carried a lot of boilerplate...) were more useful than the (pardon the editorializing) mega-clunky, native &#039;class&#039; framework in Matlab.  My latest versions are far more concise and have been optimized for performance.  This gives me &#039;real&#039; OO within the Matlab, my favorite engineering bat&#039;leth, the one that I use almost every day on my desk and in the lab.

Another neat-o-keen aspect of this approach is that classes can be defined within classes and/or multiple classes can be defined within the same .m file, rather than in the plethora of .m files within a @foo directory.

Loren&#039;s example shows how nice it is to be able to compose objects.  That way, you can cleanly encapsulate closely related responsibilities for a behavior into an individual objects and then compose several of those objects to solve a particular problem.

As a parting shot, for anyone getting into hacking OO for the first time, some excellent essays on fundamental &#039;good principles&#039; by Robert Martin can be found at: http://www.objectmentor.com/omSolutions/oops_what.html. The OO paradigm is just another tool - one can write either beautiful or hideous OO code...

I echo Sean&#039;s sentiments about GUIDE.  Building a replacement has been on my to-do list for ages and I&#039;m not surprised that he and his colleagues have done it.

Cheers and happy Matlab hacking!

Michael</description>
		<content:encoded><![CDATA[<p>Good to see that the &#8220;secret&#8221; is getting out&#8230;!</p>
<p>Like Sean, I&#8217;ve been using closures with nested functions and function handles to do &#8216;lightweight&#8217;/'pseudo&#8217; OO (lacking inheritance) since their introduction in 7.0.  I was inspired by similar patterns in Scheme. </p>
<p>I also use anonymous functions quite a bit. Incidentally, by using anonymous functions (lambda functions&#8230;) one can hack many of the examples in the first chapters of the classic, &#8220;Structure and Interpretation of Computer Programs&#8221; (<a href="http://mitpress.mit.edu/sicp/" rel="nofollow">http://mitpress.mit.edu/sicp/</a>).  This is a great way of getting a little taste of functional programming vs. the procedural, imperative paradigm that 99% of Matlabers probably are more used to.</p>
<p>A massive advantage to using closure objects can be that a function handle reference to an object (instance) method provides pass-by-reference, so, e.g., the same instance can be referenced in multiple collections.  This leads to the second massive advantage: persistence.  As long as there is a reference to the object its enclosed state exists and anyone who has a reference to it can use it.  There is no need to use globals ever again (DEATH to globals!), except, perhaps in exceptional circumstances.</p>
<p>In order to realize a closure object as a &#8216;pure&#8217; reference the structure returned by the main, enclosing function (effectively, the constructor) must be only a structure of function handles (or a single function handle pointing to a dispatcher).  Loren&#8217;s example, volumeVisualization, breaks this a bit by putting some mutable state onto the structure, but that could be fixed easily.</p>
<p>All access to internal state should be through accessor methods.  As a hint, a convenient, minimalist accessor providing both get and set is shown by this example:</p>
<pre class="code">
function obj = foo

% variables in the enclosing function are attributes
name = '';  

% provide accessors for the ones you want to be 'public'
obj.name = @accessName;
  function out = accessName(in)
    if nargin, name = in; end
    out = name;
  end

% other methods change state (provide behavior)
...

end
</pre>
<p>Then&#8230;.</p>
<pre class="code">
a = foo;
a.name('I am a foo');
a.name() -&gt; 'I am a foo'

b = a;
b.name() -&gt; 'I am a foo'   % reference to the same object
</pre>
<p>I&#8217;ve implemented several variants of OO frameworks based on closures that add inheritance.  Even the somewhat bulky, first forms (they carried a lot of boilerplate&#8230;) were more useful than the (pardon the editorializing) mega-clunky, native &#8216;class&#8217; framework in Matlab.  My latest versions are far more concise and have been optimized for performance.  This gives me &#8216;real&#8217; OO within the Matlab, my favorite engineering bat&#8217;leth, the one that I use almost every day on my desk and in the lab.</p>
<p>Another neat-o-keen aspect of this approach is that classes can be defined within classes and/or multiple classes can be defined within the same .m file, rather than in the plethora of .m files within a @foo directory.</p>
<p>Loren&#8217;s example shows how nice it is to be able to compose objects.  That way, you can cleanly encapsulate closely related responsibilities for a behavior into an individual objects and then compose several of those objects to solve a particular problem.</p>
<p>As a parting shot, for anyone getting into hacking OO for the first time, some excellent essays on fundamental &#8216;good principles&#8217; by Robert Martin can be found at: <a href="http://www.objectmentor.com/omSolutions/oops_what.html" rel="nofollow">http://www.objectmentor.com/omSolutions/oops_what.html</a>. The OO paradigm is just another tool &#8211; one can write either beautiful or hideous OO code&#8230;</p>
<p>I echo Sean&#8217;s sentiments about GUIDE.  Building a replacement has been on my to-do list for ages and I&#8217;m not surprised that he and his colleagues have done it.</p>
<p>Cheers and happy Matlab hacking!</p>
<p>Michael</p>
]]></content:encoded>
	</item>
</channel>
</rss>

