Login  Register

Re: Character>>asCharacter ?

Posted by Chris Uppal-3 on Nov 07, 2004; 9:53am
URL: https://forum.world.st/Character-asCharacter-tp3372010p3372019.html

costas menico wrote:

> It doesnt make sense to have one because asCharacter takes a character
> and answers its ascii representation, an integer.

(I'm assuming that wrong-way-around conversion in the above is a typo.)

The #as* type methods generally come in a "ring" of mutually convertible
objects.  E.g. consider asArray, asSet, asOrderedCollection, etc.  In these
cases the #as* methods allow you to treat arrays, etc, similarly without
worrying about the specific classes because #asXXX will convert it for you /if
necessary/.  That pattern would break if, for instance, Array did not implement
#asArray or if it did not answer self.  The #as* methods aren't really about
/converting/ one object into another, they are about /hiding the differences
between them/.

Now one could make an argument that Integers and Characters aren't
intra-convertible in this way.  If you take that line then integer>>asCharacter
should not exist, and neither should Character>>asInteger.

OTOH one could say that Integers and Characters are almost the same since (in
many contexts) a Character can be identified with its code-punt.  If you see it
that way, then Integer>>asCharacter and Character>>asInteger are fine, but then
you'd expect to find Integer>>asInteger and Character>>asCharacter implemented
too.  In the current Dolphin code base, the second of these is missing.

    -- chris