Re: Initing sublacss with superclass method
Fred Kiefer <[email protected]> Fri, 11 Jul 2025 18:41:12 +0200
| Newsgroups | gmane.comp.lib.gnustep.devel |
|---|---|
| Message-ID | <[email protected]> |
In GNUstep base this method is implemented in this way:
+ (NSCharacterSet*) characterSetWithCharactersInString: =
(NSString*)aString
{
NSMutableCharacterSet *ms;
NSCharacterSet *cs;
ms =3D [NSMutableCharacterSet new];
[ms addCharactersInString: aString];
cs =3D [ms copy];
RELEASE(ms);
return AUTORELEASE(cs);
}
And copy will result in a non mutable character set. But from this =
method you also see what you should have used in the first place, just =
the first two lines of this method :-)
Hope this helps,
Fred
> Am 11.07.2025 um 16:35 schrieb David Chisnall =
<[email protected]>:
>=20
> 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?
>=20
> 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
>=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.
>=20
> You can also try `NSLog(@=E2=80=9CWhat even is this? %@=E2=80=9C, =
[wsAndTagClosing class]);` to see.
>=20
> David