Re: coerce for arbitrary types
- From: rem642b@xxxxxxxxx (Robert Maas, see http://tinyurl.com/uh3t)
- Date: Fri, 04 Apr 2008 23:26:36 -0700
From: Scott Burson <FSet....@xxxxxxxxx>
If I understand the hyperspec correctly, then there is no way to extend
coerce to accept any other result-type than the ones explicetly listed.
IMO that's a good idea, because coerce is already a crock from the start.
It is not possible to change something from the type it was to the
type you want to be (although in the case of CLOS objects, you can
change the class to which an object belongs, but still you can't
change it to no longer be a CLOS object, and you can't change
anything else to become a CLOS object). So coerce must do something
sorta random, such as perform a shallow copy from one type of
container to a newly-created copy of another type, or perform a
"similar-meaning" conversion such as from an integer to a float.
Nevertheless, I would like to transform from and to arbitrary types.
That would only make the confusion worse. Better to stop using
coerce entirely. If you want a shallow copy from one type of
sequence to another, call MAP with the IDENTITY function.
(map 'list #'identity "foo")
I you want a numeric conversion, use FLOAT or ROUND or FLOOR etc.
Perhaps the generic function should be defined in its own library so
the definition can be more easily shared.
Why do you even need a generic function, instead of calling the
specific clearly-defined function for the shallow copy or numeric
near-value or whatever?
If you want to convert the first character of a string to a character:
(elt "X" 0)
If you want to convert a character to a single-character string:
(format nil "~A" #\X)
If you want to convert a string to its integer value:
(parse-integer "42")
If you want to convert a integer to its string representation:
(format nil "~D" 42)
etc. etc.
Say exactly what you want done instead of trying to design and use
some catch-all convert-type function where the reader of your code
has to guess what your function-call really does.
.
- Follow-Ups:
- Re: coerce for arbitrary types
- From: Kent M Pitman
- Re: coerce for arbitrary types
- References:
- coerce for arbitrary types
- From: Albert Krewinkel
- Re: coerce for arbitrary types
- From: Scott Burson
- coerce for arbitrary types
- Prev by Date: Re: Newbie FAQ #2: Where's the GUI?
- Next by Date: Re: Newbie FAQ #2: Where's the GUI?
- Previous by thread: Re: coerce for arbitrary types
- Next by thread: Re: coerce for arbitrary types
- Index(es):
Relevant Pages
|