[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: dynamic vs. static typing



Steve Dekorte wrote:
> On Nov 19, 2003, at 2:22 AM, Steve Dekorte wrote:
> >>> What if I want to send a message to all the objects in the list that
> >>> respond to a method, but not to those that don't? For example, let's
> >>> say I have a list of things in my house and I want to send a turnOff
> >>> message to everything that can be turned off.
> >>
> >> As Anton van Straaten explained beautifully, all the objects in this
> >> case respond to some message like "turn off if you can", or "can you
> >> turn off?".
> >
> > We'll say that all implement, say, a "respondsTo:" method (that takes
> > a string argument and returns True or False) and some (but not all)
> > implement a turnOff method.
> >
> > Can we have a type that declares that an object implements respondsTo:
> > and *may* implement turnOff? If so, how does it prevent the
> > possibility of a "does not respond to turnOff" error?
>
> I noticed no one responded to this post of mine. Was it a silly
> question or a difficult one for ST? :-)

I responded indirectly, but to be more specific, you could certainly type a
list as supporting 'respondsTo:' and use that information to filter the list
at runtime.

This kind of thing is not unusual e.g. in C++ using Microsoft's COM, where
one might iterate through a list using the QueryInterface method of the
IUnknown interface to determine which objects respond to the interface
you're looking for - in this case, the one containing 'turnOff'.
(QueryInterface is a very close analog of 'respondsTo:')

Note that although this is fundamentally a dynamic operation - in that the
check is occurring at runtime - it's being done in a statically typed
language, and doesn't violate static typing, although it does require the
ability to perform casts between types to be useful.

I'll construct a cute little straw man and suppose a response to the above
of "Aha!  You're not statically ensuring that values are of the right type!"
But this response would miss the point.  The point is not that everything
imaginable can be known statically - if it could, we might not need to run
our programs at all.  The point is that if something *can* be known
statically, then it can be beneficial to express that and check it
statically, for a variety of reasons.

An ideal typesystem should allow static expression or detection of the
things that can be known statically, without significantly inhibiting our
ability to manipulate things that can only be known dynamically.  Pure DT
languages say "we dunno how to provide that, so we're not going to offer any
static analysis whatsoever".  I don't believe that this choice, as pragmatic
as it may be, represents the ultimate in language capabilities.

Anton