Let’s say we want a type A[T]
, invariant in T
, and we want to be able to express its greatest lower bound, the intersection of A[T]
for all T
.
Is the code below a good trick to do so? Does it always work?
type A[T] = Helper[T, T]
// ABot <: A[T] holds true for any T
type ABot = Helper[Nothing, Any]
trait Helper[+P, -N]
That would be a lower bound, but not necessarily the greatest.
In order to get the greatest, you need a language like Idris or Lean that supports Product types over a family of types.
If you want to simulate the closest thing Scala can do, I guess this is not too bad; although Helper[Nothing, Any]
is not all that meaningfully different than just Nothing
in my opinion.
I couldn’t find any greater lower bound expressible in Scala, or even a type situation where it would behave differently from the greatest lower bound; though admittedly I didn’t spend that much time on it.
If it really is indistinguishable from the greatest lower bound in Scala, then I think it’s fair to identify the two.
Nothing
can behave quite differently, as it is the universal least lower bound. It’s also special (including in how it’s treated by type inference), as we discussed in the past. It’s really the same difference as between the lowest upper bound of A[T]
and Any
.
I’m not so sure about that; you might run into similar type narrowing / widening problems with Helper
.