Andreas had this to say about testing for the presence of a class:
It seems that:
- first it would be better to let the compiler compiles method with
reference to classes that are not in the system and let the system complain at execution tim (marcus suggestion and he can explain that much better than me).
I doubt this. Yes, all of these could be re-written as, say:
Foo ifNil:[self mumble]. Foo ifNotNil:[Foo bar].
instead of
Smalltalk at: #Foo ifAbsent:[self mumble] Smalltalk at: #Foo ifPresent:[:foo| foo bar].
but the main point is that the latter two are very verbose in conveying the intention that "yes, the caller is very well aware that Foo may be absent and that the dependency is a weak one". If you know how to read to the first one the same may be true but the latter is just simply obvious in this regard.
You know, neither of these is really very satisfying. The first is ambiguous. There are any number of reasons why Foo could be nil, and a weak dependency is only one of them. My problem with the second one isn't so much the verbosity, it's that it also says "yes, the caller is well aware that class lookups go through a single global dictionary called Smalltalk." That is, it knows too much about the implementation of the compiler and runtime.
I think it would be nice to have something like this:
Foo ifPresent: [:foo | foo bar] ifAbsent: [self mumble]
This idiom clearly says what it means, "the dependency is a weak one" without saying more than that.
Colin