インスタンスが属するクラスをあとから変更する操作を Squeak Smalltalk で

Object subclass: #Cartesian
    instanceVariableNames: 'x y'

Cartesian >> x
    ^x

Cartesian >> y
    ^y

Cartesian >> setX: newX y: newY
    x := newX.
    y := newY

Cartesian class >> newFrom: aPolar
    ^self new
        setX: aPolar r * aPolar theta cos
        y: aPolar r * aPolar theta sin; yourself

Cartesian class >> x: x y: y
    ^self new setX: x y: y; yourself
Object subclass: #Polar
    instanceVariableNames: 'r theta'

Polar >> r
    ^r

Polar >> theta
    ^theta

Polar >> setR: newR theta: newTheta
    r := newR.
    theta := newTheta

Polar class >> newFrom: aCartesian
    ^self new
        setR: (aCartesian x squared + aCartesian y squared) sqrt
        theta: (aCartesian y arcTan: aCartesian x); yourself

Polar class >> r: r theta: theta
    ^self new setR: r theta: theta; yourself
| pos1 pos2 |
pos1 := pos2 := Polar r: 2 sqrt theta: Float pi / 4.
pos1 class.   "=> Polar "

pos1 become: (pos1 as: Cartesian).
pos1 class.   "=> Cartesian "
pos1 x.       "=> 1.0 "
pos1 y.       "=> 1.0 "
pos2 class.   "=> Cartesian "

pos1 become: (pos1 as: Polar).
pos1 class.   "=> Polar "
pos1 r.       "=> 1.414213562373095 "
pos1 theta / Float pi   "=> 0.25 "