Why cant list.head return an option instead of having a headOption method

It does makes sense that head of a list may or may not be there. Isn’t that what Option is for? Then why is it that head throws an exception? Can’t it just return a Option?

1 Like

If we could redesign the collections from scratch, yes, head would return an option and we would also have a headPartiallyDefined, which would behave like the current head (and is useful for performance reasons). So, in the end we still want to have both versions.
Now, head and headOption are well established. If we want to migrate to new operation names we have to do it in several steps, each step corresponding to a major Scala version: (1) introduce headPartiallyDefined and deprecate head, (2) remove head, (3) re-introduce it with Option as its return type and deprecate headOption, (4) remove headOption.
I don’t think it’s worth the effort.

3 Likes

@fhalde Beyond what @julienrf wrote, WartRemover can give you a warning if you call those methods (search for TraversableOps on http://www.wartremover.org/doc/warts.html).