Re: Initing sublacss with superclass method

Wolfgang Lux <[email protected]> Wed, 23 Jul 2025 17:30:17 +0200
Newsgroups gmane.comp.lib.gnustep.devel
Message-ID <[email protected]>
> Riccardo Mottola wrote:
>>     if ([cs isKindOfClass:[NSCharacterSet class]])
>>       NSLog(@"is NSCharacterSet");
>>     if ([cs isKindOfClass:[NSMutableCharacterSet class]])
>>       NSLog(@"is NSMutableCharacterSet");
>=20
> if this is a safe test - and it ought to be - then it passes also on =
venerable 10.4. So actually it is there that the Xcode doc. lies, since =
it returns a NSMutableCharacterSet.

Sorry, but no. Xcode doesn't "lie" here.

First of all, it's the essence of subtyping polymorphism in OO languages =
that you are free to return an object of any subclass of the method's =
declared return type. Since any NSMutableCharacterSet is also a =
NSCharacterSet, there is nothing wrong with returning a mutable =
character set when the method's return type is NSCharacterSet. The =
documentation also nowhere claims that any of these methods returns an =
object that is an instance of the class NSCharacterSet. (The old =
OpenStep documentation even explicitly says that these methods return an =
instance of some private subclass of NSCharacterSet.)

But anyway, the different return types do make sense here, since Apple's =
Foundation framework does remember whether you've requested a mutable or =
an immutable character set. Just try and cheat pretending the methods do =
actually return a mutable character set, e.g.,
  [(NSMutableCharacterSet *)[NSCharacterSet decimalDigitCharacterSet] =
addCharactersInString: @"abcdef"];
and see what happens. :-)

Wolfgang