Scala 3:使用实验类型

发布于 2025-02-12 02:18:11 字数 1109 浏览 0 评论 0原文

以下Scala 3代码以下文档效果很好

import scala.compiletime.ops.string.*
@main def refinedTypeInAction: Unit = 
  val hello: "hello " + "world" = "hello world"
  println(hello)

,但是,下面的一个

import scala.annotation.experimental
@experimental  
object UseExperimental:      
  def x: scala.compiletime.ops.string.Substring["hamburger", 4, 8] = "urge"

列出了编译时间错误,如下所示,

[error] -- [E008] Not Found Error: /Users/viswanath/projects/myproject/src/main/scala/RefinedType.scala:16:38 
[error] 16 |  def x: scala.compiletime.ops.string.Substring["hamburger", 4, 8] = "urge"
[error]    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |   type Substring is not a member of object scala.compiletime.ops.string
[error] one error found

我确实指的是 https://docs.scala-lang.org/scala3/reference/other-new-features/other-new-features/experiment-defs.html 但是我是努力克服汇编错误。请帮忙!

The following scala 3 code following documentation works fine

import scala.compiletime.ops.string.*
@main def refinedTypeInAction: Unit = 
  val hello: "hello " + "world" = "hello world"
  println(hello)

However, the one below

import scala.annotation.experimental
@experimental  
object UseExperimental:      
  def x: scala.compiletime.ops.string.Substring["hamburger", 4, 8] = "urge"

throws compile time error as shown below

[error] -- [E008] Not Found Error: /Users/viswanath/projects/myproject/src/main/scala/RefinedType.scala:16:38 
[error] 16 |  def x: scala.compiletime.ops.string.Substring["hamburger", 4, 8] = "urge"
[error]    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |   type Substring is not a member of object scala.compiletime.ops.string
[error] one error found

I did refer to https://docs.scala-lang.org/scala3/reference/other-new-features/experimental-defs.html but I'm struggling to get past the compilation error. Please help!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文