compiltime.constvalue [t]在Scala 3中的对面

发布于 2025-01-27 02:21:38 字数 344 浏览 4 评论 0原文

如果我们有一个透明的内联def f(...):boolean = ...,是否可以将f的结果转换为true代码>或false 类型,假设f的结果是在编译时知道的吗?我想在隐式搜索中使用这些类型。

但是

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

,问题是f(5)是一个值,而不是类型。因此上述代码不会编译。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落墨 2025-02-03 02:21:38

我猜最接近的是

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

我们无法替换(使用Xt&lt;:&lt; a)(使用TypeOf(f(f(5))。t&lt;:&lt; a)因为路径依赖类型即使在Scala 3中也必须具有稳定的前缀

。 -expression-in-scala“>如何获得Scala中的(静态)表达式的类型?

type注释通过推论表达式类型

Scala定义自定义类型 - 类型不匹配错误

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文