On Tue, Mar 1, 2022 at 4:51 PM Steven D'Aprano <steve@pearwood.info> wrote:
Then Python is the wrong language for you, because it uses exceptions to
direct control flow *wink*

The iteration protocol uses StopIteration to end iteration. The older
sequence protocol uses IndexError for the same purpose.

I think it's better to say that exceptions are used in these cases to cover for the lack of disjoint-union types.

Many functions return "a value or no value". When the values they can return are limited, a value they can't return is often used as a stand-in for "no value", like None or NotImplemented, or -1 for find(). When that isn't the case, an exception tends to be used, although letting the caller choose a sentinel is also popular.

Haskell tends to use Maybe in all of these cases, because it's a disjoint union: the no-value return can't overlap with the yes-value returns, so there's never a need for anything else. (And it forces you to unwrap the returned value, so you can't forget to check for the no-value case.)

The MyBreak proposal doesn't really fit that pattern. I've never seen it used, and I feel like it would be rejected in most places that have style guides and code review, but perhaps I'm wrong.