Re: coerce for arbitrary types



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.
.



Relevant Pages

  • Re: coerce for arbitrary types
    ... coerce to accept any other result-type than the ones explicetly listed. ... to infer "intent" because the fact of a representation's use is not ... different things depending on the intention of the programmer as ... parsing from a string, and no further processing was done after ...
    (comp.lang.lisp)
  • Re: Another VBA bug
    ... string, which it does however surprising at first it may seem. ... Dim dbl As Double ... ' can coerce but can't evaluate ... In Integer Division, if A and/or B are floating point numbers, ...
    (microsoft.public.excel.programming)
  • Re: Another VBA bug
    ... to your Format example, ie an exact .5 always rounds up. ... rather coerce to a valid number. ... string, which it does however surprising at first it may seem. ... dbl = CDate' OK' ...
    (microsoft.public.excel.programming)
  • Re: Another VBA bug
    ... string, which it does however surprising at first it may seem. ... dbl = CDate' OK' ... 'can only coerce to variable declared 'As Date' ... Using CDbl specifically casts 's' as a Double before the assignment is made. ...
    (microsoft.public.excel.programming)
  • Re: Another VBA bug
    ... d = s ' try to coerce ... Function IsDigitsOnly(Value As String) As Boolean ... ' Get local setting for decimal point ...
    (microsoft.public.excel.programming)