Re: Function calling
Re: Function calling
- Subject: Re: Function calling
- From: Thomas Davie <email@hidden>
- Date: Thu, 26 May 2005 02:30:22 +0100
On May 26, 2005, at 2:23 AM, Ondra Cada wrote:
Thomas,
On 26.5.2005, at 2:56, Thomas Davie wrote:
Quite agree - the part I was disputing was ``The object knows.
That's called [...] "polymorphism".''
The part was completely right. What is polymorphism?
The point I was making was that the objects need know absolutely
nothing - if the checks are done at compile time (and vice-versa).
Polymorphism in fact means that you can send the same message to
different objects, and they would behave accordingly. With strong
typing, you just limit "different objects" to subclasses of some
class, or to implementers of some protocol. (Or, in other
langauges, to other similar primitives, but I thought we are
discussing ObjC here, are we not?)
For example, in a plain function
int sum(id o) {
int sum=0;
for (NSEnumerator *en=[a objectEnumerator];o=[en nextObject];) sum
+=[o intValue];
return sum;
}
could we perhaps benefit from strong typing, having the compiler to
check that the argument is an NSArray and its items are NSNumnbers?
Nope, we could not -- actually, the very opposite: it would
*cripple* the very idea of polymorphism. The way it is written now
it would work all right with a set containing NSStrings, or, with a
few simple categories, with, say, an NSImage and its NSImageReps or
with an NSDocument and its NSWindowControllers -- or, truly, with
*whatever*.
That is polymorphism at its best: the fact we can send *any* object
messages "objectEnumerator" or "intValue" and presume it would
behave accordingly (which may also mean "reports and error": that's
definitely an appropriate reaction in case the object cannot
support the appropriate service). The strong typing just *limits*
polymorphism, never ever helps it.
sum :: Num a => [a] -> a
sum = foldr (+) 0
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f x [] = x
foldr f x (y:ys) = f x (foldr f y ys)
Strongly typed and as polymorphic as yours.
Bob
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden