Re: coerce for arbitrary types
- From: Kent M Pitman <pitman@xxxxxxxxxxx>
- Date: 05 Apr 2008 11:11:07 -0400
rem642b@xxxxxxxxx (Robert Maas, see http://tinyurl.com/uh3t) writes:
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.
Why do you need to do this? This is often a sign of a program being
structured wrong--trying to get too much information out of too few bits.
http://www.nhplace.com/kent/PS/EQUAL.html#two-bit_rule
In fact, the entire article
http://www.nhplace.com/kent/PS/EQUAL.html
in which that rule resides builds up to the relevant point, which is
that in dynamic languages, additional care is required in order to
infer "intent" because the fact of a representation's use is not the
same as the intent of a representation's use, and you can only
dynamically query about the fact of its use, not the intent. This is
not a bug in dynamic languages because it buys additional, much needed
flexibility in other situations. But in the case here, you have to be
careful to either pass more information or not to infer so much information
from what is passed. Sometimes more information means "another argument"
and sometimes it means "a different representation of the data than the
one you're trying to coerce". So, for example:
IMO [not allowing coerce to be extended is] 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
This isn't the reason it's a "crock". That's just the reason it has a
bad name. The definition has issues that make it not easily
extendable independent of its name, and those issues derive from the
nature of the language. In brief, COERCE is too short a name and
falsely gives the sense that it is a canonically defined operation
with only one meaning rather than an operation that has several
(perhaps myriad) friends that are all vying for the cool short-name.
If these were domain names, not function names, all the names
coerce.org, coerce.net., coerce.cc, coerce.tv, etc. would all be
bought up and people would be assigning them subtly different
meanings. But when there's no trailing GTLD to remind us, it's easy
to forget that there was a competition in play.
The real problem is that something you can customize needs to be
defined in such a way that it's clear what customizing it would mean.
For historical reasons, COERCE is already messed up because of the NIL
vs () issue [note well: NOT the issue commonly fussed over, which is
the false vs. empty list issue]. The simple case of:
(coerce nil 'string) => ""
(string nil) => "NIL"
illustrates the problem.
The reason this happens by the way is that STRING plainly does not work
on arbitrary sequences, so there's no confusion that STRING is not asking
to operate on sequences. e.g.,
(string #(#\N #\I #\L))
Error: Cannot coerce #(#\N #\I #\L) to type STRING.
So it was deemed necessary that STRING should lean toward seeing NIL as
its name, which was a string. But COERCE does take sequences, and empty
sequences were common and useful. The progression
(coerce '(a b) 'vector)
=> #(A B)
(coerce '(a) 'vector)
=> #(A)
(coerce '() 'vector)
=> #(#\N #\I #\L)
would have been devastating to getting any work done. So COERCE was defined
to treat NIL as () rather than as a symbol.
In effect, you are meant to make the choice between STRING and
(COERCE x 'STRING) somewhat on the basis of understanding how you want this
particular tie to be broken.
But if we were to make it open season on extending this, in the present of
a dynamic language with multiple inheritance, this kind of problem would
happen all the time, and the result would be ragged... absent the intent
information I alluded to above.
.
- Follow-Ups:
- Re: coerce for arbitrary types
- From: Robert Maas, see http://tinyurl.com/uh3t
- Re: coerce for arbitrary types
- From: Albert Krewinkel
- Re: coerce for arbitrary types
- References:
- coerce for arbitrary types
- From: Albert Krewinkel
- Re: coerce for arbitrary types
- From: Scott Burson
- Re: coerce for arbitrary types
- From: Robert Maas, see http://tinyurl.com/uh3t
- coerce for arbitrary types
- Prev by Date: Re: Lisp group moderated
- Next by Date: Re: Lisp power pack
- Previous by thread: Re: coerce for arbitrary types
- Next by thread: Re: coerce for arbitrary types
- Index(es):
Relevant Pages
|