gnustep / libs-base

The GNUstep Base Library is a library of general-purpose, non-graphical Objective C objects.
https://www.gnustep.org/
GNU General Public License v2.0
932 stars 279 forks source link

`-[NSArray removeLastObject]` throws exception when array is empty #432

Closed ERobsham closed 1 month ago

ERobsham commented 1 month ago

The GNUStep implementation of -[NSArray removeLastObject] differs from the Apple implementation. In GNUStep, exceptions are thrown when an array is empty and the -removeLastObject method is called:

- (void) removeLastObject
{
  NSUInteger    count = [self count];

  if (count == 0)
    [NSException raise: NSRangeException
         format: @"Trying to remove from an empty array."];
  [self removeObjectAtIndex: count-1];
}

In Apple implementations, the -removeLastObject method just silently exits when there are no elements to remove.

To improve compatibility for codebases used across both implementations, I think this exception raise should be dropped in favor of just a return.

ERobsham commented 1 month ago

Changes merged to prevent raising exceptions with empty arrays -- closing.