2

If we have a transparent inline def f(...): Boolean = ..., is it possible to convert the result of f to true or false types, assuming the result of f is known at compile-time? I would like to use those types in an implicit search.

For example,

given [A](using f(5) <:< true): MyTypeClass with ...

However, the problem is that f(5) is a value, not a type. So the above code won't compile.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Koosha
  • 1,492
  • 7
  • 19

1 Answers1

0

I guess the closest is

val x = TypeOf(f(5))

given [A](using x.T <:< A): MyTypeClass[A] = ???

class TypeOf[A](a: A):
  type T = A

trait MyTypeClass[A]

transparent inline def f(inline i: Int): Any = inline i match
  case 5 => true
  case _ => "a"

summon[MyTypeClass[Boolean]] // compiles
summon[MyTypeClass[String]] // doesn't compile

Unfortunately we can't replace (using x.T <:< A) with (using TypeOf(f(5)).T <:< A) because path-dependent types must have stable prefix even in Scala 3.

How to get the (static) type of an expression in Scala?

type annotations overriden by inferred expression type

Scala Defining Custom Type - Type Mismatch Error

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66