| Sean Millichamp That is what I am leaning towards (not changing) since "give me a unique set of undef" is a strange thing to ask for and most likely masks some other problem if it was to return undef. We had a similar request to make empty() accept undef and return true - there we had lots of breakage of legacy code when it did not accept undef, and it seemed reasonably harmless to allow that even if you can argue that undef is indeed undefined and you cannot even know if it is something "empty or not". OTOH, undef is often referred to as "the empty set". In this case (for unique) this ticket was filed long after 5.0.0 was released - so cannot be a common problem and is easily avoided by using type checking. You can argue that if undef is indeed "the empty set", then a unique should return that empty set - e.g. an empty array. But, if undef represents the "empty set", then it would be reasonable to return undef - but the problem with returning undef is that it should return something that is iterable (for example a String if given a String, and that works also on an empty string). So to comply with that requirement it would need to return an empty iterable (for example an empty Array). So, my position is:
- I don't like the idea of turning undef into an Array since I think this masks a problem
- I don't like the idea of turning undef into undef since it will move the error to where you try to iterate over the undef result - so this goes against "catch the error early"
- I don't like making undef iterable as if it was an empty array - I think that would just mask other errors and move the problem making it more difficult to find the cause.
If accepting something like Optional[Array] and wanting to do a unique it is easy to use conditional logic or calling then
# Three different ways to check with conditional logic |
$result = if $maybe_an_array =~ Array { $maybe_an_array.unique } # results in undef if not an array |
$result = $maybe_an_array ? { Array => $maybe_an_array.unique, default => undef } |
$result = $maybe_an_array.then |$x| { $x.unique }
|
which all gives you undef for undef and the call to unique otherwise. That is a long way of arriving at: closing this as "won't do". |