[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: dynamic vs. static typing
On 19 Nov 2003, at 8:01, Ken Shan 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?".
...but that's a pretty bad idea in general. When you say this...
if (object.turnOffSupported()) {
object.turnOff();
}
...there is a chance that the object is replaced in another thread
between the check and the actual call. The better solution is this...
try {
object.turnOff();
} catch (OperationNotAvailableException e) {
...
}
...because now you have (more or less) exactly one point in time at
which the check and the invocation are performed simultaneously. [1]
BTW, this is what a DT language gives you for free. ;)
Pascal
[1] And _please_ make this an unchecked exception. Checked exceptions
suck very badly.
--
Tyler: "How's that working out for you?"
Jack: "Great."
Tyler: "Keep it up, then."