[squeak-dev] Re: Changing an object's class

Andreas Raab andreas.raab at gmx.de
Mon May 2 05:29:01 UTC 2011


On 5/2/2011 2:04, Levente Uzonyi wrote:
> On Sun, 1 May 2011, Chris Muller wrote:
>
>>> So I thought, well, wouldn't it be nice if one could _change the
>>> class_ of
>>> the MutableZTrees so they all magically turn into nice immutable ZTrees.
>>
>> Object>>#primitiveChangeClassTo:
>>
>>
>
> It doesn't work in cases like:

Indeed. It's all in the method comment:

primitiveChangeClassTo: anObject
	"Primitive. Change the class of the receiver into the class of the 
argument given that the format of the receiver matches the format of the 
argument's class. Fail if receiver or argument are SmallIntegers, or the 
receiver is an instance of a compact class and the argument isn't, or 
when the argument's class is compact and the receiver isn't, or when the 
format of the receiver is different from the format of the argument's 
class, or when the arguments class is fixed and the receiver's size 
differs from the size that an instance of the argument's class should have.
	Note: The primitive will fail in most cases that you think might work. 
This is mostly because of a) the difference between compact and 
non-compact classes, and b) because of differences in the format. As an 
example, '(Array new: 3) primitiveChangeClassTo: Morph basicNew' would 
fail for three of the reasons mentioned above. Array is compact, Morph 
is not (failure #1). Array is variable and Morph is fixed (different 
format - failure #2). Morph is a fixed-field-only object and the array 
is too short (failure #3).
	The facility is really provided for certain, very specific applications 
(mostly related to classes changing shape) and not for casual use."

Cheers,
   - Andreas

>
> #[65 66 67] primitiveChangeClassTo: ByteString. "==> primitiveFailed"
>
> | b |
> b := #[65 66 67].
> ByteString adoptInstance: b.
> b "==> 'ABC'"
>
> And it's probably broken too:
>
> Object new primitiveChangeClassTo: ProtoObject. "==> primitiveFailed"
>
>
> Levente
>
>




More information about the Squeak-dev mailing list