scala 3 内联失败了一个非常简单的例子

问题描述

我正在探索 scala3 内联的可能性。我已经编写了一个简单的示例,我希望在编译时应用构造密封类的验证方法

import scala.compiletime.{ error,codeOf }

sealed abstract class OneOrTwo(val code: String)

object OneOrTwo {

    case object One extends OneOrTwo("one")
    case object Two extends OneOrTwo("two")

    def from(s: String): Option[OneOrTwo] = 
        s match {
            case One.code => Some(One)
            case Two.code => Some(Two)
            case _ => None
        }

    inline def inlinedFrom1(s: String): OneOrTwo = {
        inline s match {
            case "one" => One
            case "two" => Two
            case _ => error("can't make a OneOrTwo out of " + codeOf(s))
        } 
    }

    val test1 = OneOrTwo.inlinedFrom1("one") // compiles 
    val test12 = OneOrTwo.inlinedFrom1("three") // doesn't compile as expected -> can't make a OneOrTwo out of "three"
}

到目前为止,一切都很好。但我真正想要的是能够在内联函数中重用 from 函数。这是我在尝试:

    // this goes in the OneOrTwo companion object as well

    inline def inlinedFrom2(s: String): OneOrTwo = {
        from(s).getorElse(error("can't make a OneOrTwo out of " + codeOf(s)))
    }

    inline def inlinedFrom3(s: String): OneOrTwo = {
        s match {
            case One.code => One
            case Two.code => Two
            case _ => error("can't make a OneOrTwo out of " + codeOf(s))
        }
    }

    
    val test2 = OneOrTwo.inlinedFrom2("one") // doesn't compile -> can't make a OneOrTwo out of "one"
    val test3 = OneOrTwo.inlinedFrom3("one") // doesn't compile -> can't make a OneOrTwo out of "one"

inlinedFrom3 是一种概括,表明如果我直接匹配对象的代码,编译器不会看到它们与输入字符串相同并采用错误的分支。

我想了解的是为什么 inlinedFrom3 不起作用以及是否有办法让它起作用。

注意:我使用的是 Scala 3.0.0-RC2

解决方法

好问题,

首先,考虑内联是在 Scala 编译时执行元编程的一种方式(正如您在示例中所做的那样)。

为了解决您的问题,这里编译器不理解 One.code"one" 类型。 实际上,如果您尝试打印 One.code,编译器输出是:

OneOrTwo.One.code

而不是

"one"

但是等等,为什么?因为编译器在编译时无法推断任何有关 One.code 的信息。要将缺失的信息提供给编译器,您可以:

  • 指定单例字符串类型
sealed trait OneOrTwo {
  val code: String
}

object OneOrTwo {
    case object One extends OneOrTwo {
      override val code : "one" = "one" 
    }
    case object Two extends OneOrTwo {
      override val code : "two" = "two"
    }
}
  • 内联 val 定义
sealed trait OneOrTwo {
  val code: String
}

object OneOrTwo {
    case object One extends OneOrTwo {
      override inline val code = "one" 
    }
    case object Two extends OneOrTwo {
      override inline val code = "two"
    }
}

即使进行了这种修改,您的代码仍然在编译中失败。这是由未内联的匹配子句引起的。事实上,正如前面提到的 hereerror 方法提供了一种发出自定义错误消息的方法。如果对错误的调用被内联而不是作为死分支消除,则会发出错误。 因此,如果您将 error(...) 放在未内联的代码中,将始终抛出错误。 例如,这段代码:

if(false) { error("thrown..") } { }

产生编译错误。

最后,为了解决您的问题,您需要内联您的 match 子句:

inline def inlinedFrom3(s: String): OneOrTwo = {
   inline s match {
       case One.code => One
       case Two.code => Two
       case _ => error("can't make a OneOrTwo out of " + codeOf(s))
   }
}

编译(inlinedFrom3)成功。由于上述问题,inlineFrom2 继续失败。

希望能帮到你。