Re: Initing sublacss with superclass method
David Chisnall <[email protected]> Fri, 11 Jul 2025 15:35:28 +0100
| Newsgroups | gmane.comp.lib.gnustep.devel |
|---|---|
| Message-ID | <[email protected]> |
On 11 Jul 2025, at 14:17, Riccardo Mottola <[email protected]> = wrote: >=20 > Hi, >=20 > In Grr there is this code: >=20 > wsAndTagClosing =3D [NSMutableCharacterSet > characterSetWithCharactersInString: @"/>"]; >=20 > Clang warns me with: >=20 > NSString+TolerantHTML.m:109:21: warning: incompatible pointer types > assigning to 'NSMutableCharacterSet *' from 'NSCharacterSet *' > [-Wincompatible-pointer-types] > 109 | wsAndTagClosing =3D [NSMutableCharacterSet > characterSetWithCharactersInString: @"/>"]; >=20 > GCC gives a more generic issue about "distinct objective C type". >=20 > Now, characterSetWithCharactersInString: is a class method of > NSCharacterSet: >=20 > = https://www.gnustep.org/resources/documentation/Developer/Base/Reference/N= SCharacterSet.html#method$NSCharacterSet+characterSetWithCharactersInStrin= g$ >=20 > and NSMutableCharacterSet is a subclass of NSCharacterSet >=20 > Shouldn't thus the method called on the subclass still return a valid > subclass and not the superclass? >=20 > Could it be a header issue? we define it as: > + (NSCharacterSet*) characterSetWithCharactersInString: = (NSString*)aString; >=20 > maybe it should return instancetype? Factory methods on the superclass, if called on the subclass, are not = required to return an instance of the subclass, they can return a = totally unrelated subclass. For example, a bunch of the NSString and = NSDictionary factory methods will return a specialised version for small = of arguments. If you=E2=80=99re doing this kind of trick, you need an = explicit check in the method implementation to see if `self` is the = expected class. =20 If that check exists, or if this method isn=E2=80=99t returning a = subclass instance, it=E2=80=99s safe to turn it into instancetype and = this warning will go away. If not, then this is a real warning and the = mutable subclass is not being returned. You can also try `NSLog(@=E2=80=9CWhat even is this? %@=E2=80=9C, = [wsAndTagClosing class]);` to see. David