Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat do you call a method of an object that changes its class?
    text
    copied!<p>Let's say you have a <code>Person</code> object and it has a method on it, <code>promote()</code>, that transforms it into a <code>Captain</code> object. What do you call this type of method/interaction?</p> <p>It also feels like an inversion of:</p> <pre><code>myCaptain = new Captain(myPerson); </code></pre> <p><strong>Edit:</strong> Thanks to all the replies. The reason I'm coming across this pattern (in Perl, but relevant anywhere) is purely for convenience. Without knowing any implementation deals, you could say the <code>Captain</code> class "has a" <code>Person</code> (I realize this may not be the best example, but be assured it isn't a subclass).</p> <p>Implementation I assumed:</p> <pre><code>// this definition only matches example A Person.promote() { return new Captain(this) } personable = new Person; // A. this is what i'm actually coding myCaptain = personable.promote(); // B. this is what my original post was implying personable.promote(); // is magically now a captain? </code></pre> <p>So, literally, it's just a convenience method for the construction of a <code>Captain</code>. I was merely wondering if this pattern has been seen in the wild and if it had a name. And I guess yeah, it doesn't really change the class so much as it returns a different one. But it theoretically could, since I don't really care about the original.</p> <p>Ken++, I like how you point out a use case. Sometimes it really would be awesome to change something in place, in say, a memory sensitive environment.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload