Open neko-kai opened 2 years ago
The lub
can be computed as follows types.foldLeft(TypeRepr.of[Nothing])(OrType)
.
And likewise, the glb
can be computed as types.foldLeft(TypeRef.of[Any])(AndType)
. ;)
@nicolasstucki @sjrd Technically correct, sure.
I mean the analogue of lub
as in Scala 2's reflection API and in Scala 3's type inference - widened to drop unions. e.g. lub[Int, Long] = AnyVal
The reflection API does not give access to type inference, because type inference is not specified and could therefore break at any time.
And outside of type inference, the lub/glb notion from Scala 2 has no equivalent/is irrelevant in Scala 3. It was necessary in Scala 2 because it did not have union and intersection types. In the Scala 3 type system, by definition, lub and glb are |
and &
.
@sjrd Well, in the case where I need lub I'm interacting with the Java type system, where such notions are still relevant. I'm extracting the common base type of an intersection type, then extracting its base class.
type inference is not specified and could therefore break at any time.
That's just an argument, if far-fetched, for having such a function - if you were splicing code that relied on current widened type inference, you'd want to compute types manually to make sure it still works if inference surfaces unions in the future.
Or if implicit search surfaces unions in the future. I'm using implicit search to compute non-union LUB in one part of library API, if implicit search were to surface unions in the future too, I'd like to still retain the ability to replace that part with a macro.
Compiler version
3.1.2
Minimized code
Output
Expectation
We need a way to compute the LUB of a set of types to port features of
izumi-reflect
to Scala 3Original issue: https://github.com/zio/izumi-reflect/issues/133