Pre-SIP: A New Type for Optionals and Error Handling (2)

Getting back to the type inference/typeclass issue (regarding Bifunctor etc)

Yes, that’s exactly how it works. I had an LLM coding assistant implement it on top of the current prototype: Commits · mberndt123/scala3 · GitHub

Maybe is now Maybe[+E, +T], A ? E is typed as Maybe[E, A], and A? as Maybe[Unit, A]. The syntax doesn’t change at all. The left-bias special case in TypeComparer is gone: plain old right-biased inference gives [X] =>> X ? E for F[_], and F := Maybe for F[_, _], so Bifunctor and friends see the same order as for Either. Most of the compiler already went through a single extractor for maybe types, so apart from the library the diff is small.

The branch also makes kind-projector syntax work, with placeholders numbered in application order:

given Bifunctor[* ? *] = ...
given [E] => Functor[* ? E] = ...

* ? * is literally Maybe, so summon[Bifunctor[* ? *] =:= Bifunctor[Maybe]] compiles.

So it’s a purely syntactic rule applied in exactly one place, and type inference needs no special cases at all. Unless someone sees a problem I’m missing, I think this is the way to go.

(disclosure: LLM assistance was used for this comment)

5 Likes