Add an API for getting an erased type #209
Comments
That seems to exclude too much and not enough things at the same time. For example, Incidentally, arrays are also why we can't directly expose our internal type erasure method: internally we represent arrays after erasure as a I don't know what your usecase is exactly, but could you emit from your macro a call to |
ah I found your usecase in https://github.com/softwaremill/tapir/blob/scala3/core/src/main/scala-3/sttp/tapir/macros/TapirMacros.scala: inline def oneOfMapping[T: ClassTag](statusCode: StatusCode, output: EndpointOutput[T]): OneOfMapping[T] =
tapirMacros.oneOfMappingImpl[T]('statusCode, 'output, '{classTag[T]}) } which calls '{ sttp.tapir.oneOfMappingClassMatcher($statusCode, $output, $ct.runtimeClass) } which calls OneOfMapping(Some(statusCode), output, { (a: Any) => runtimeClass.isInstance(a) }) so it seems to me that you could drop the macro and just rely on the power of inline: inline def oneOfMapping[T](statusCode: StatusCode, output: EndpointOutput[T]): OneOfMapping[T] =
OneOfMapping(Some(statusCode), output, (a: Any) => a.isInstanceOf[T]) and let the compiler warn the user if the type test cannot be checked at runtime. /cc @adamw |
To summarize the discussion, adding this API would be non-trivial and it seems that the usecase suggested for it would be better served by judicious use of inline, so I will be closing this issue, please reopen if I missed something. |
We ended up using the heuristics: TypeRepr.of[T] match {
case _: AppliedType | _: AndOrType => false
case _ => true
} so that we could issue a compiler error. As these are quite typical warnings (e.g. in pattern matches), they might be easily overseen. |
That's bad since all unchecked warnings are equally problematic, they should either be silenced with |
(I also mentioned above how that heuristic is incorrect) |
Also, I'd be happy with making unchecked warnings errors by default, but I suspect that would require some discussions first. |
That's true, but practice often diverges from the preferred state :) |
In Scala 2, there is a method
TypeApi#erasure
, which is very useful in derivation pre-checks (e.g. https://github.com/softwaremill/tapir/blob/master/core/src/main/scala/sttp/tapir/internal/OneOfMappingMacro.scala). We would love to have a similar method in dotty (e.g. defined forTypeRepr
orType[_]
), in order to let the users known, that the derivation may not be possible in a certain case.Workaround
As far as I understand, the currently available substitute for
is
The text was updated successfully, but these errors were encountered: