You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala 3不透明类型的内联扩展编译报错问题咨询

Scala 3内联扩展+不透明类型组合导致的编译错误问题

问题复现

复现该问题需要三个文件:

Part A

class ImplA:
  def doIt(): Boolean = false

opaque type A <: AnyRef = ImplA

object A:
  extension (inline x: A)
    inline def fun(): Boolean = x.doIt()

Part B

class ImplB extends ImplA

opaque type B <: A = ImplB & A

Part C

def funB(x: B): Unit =
  println(x.fun())

编译错误信息

上述代码无法编译,报错如下:

E050] Type Error: ...
  println(x.fun())
          ^^^^^^^
          expression does not take parameters
----------------------------------------------------------------------------
Inline stack trace
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This location contains code that was inlined from ProblemPartA.scala:12
    inline def fun(): Boolean = x.doIt()
                                ^^^^^^
----------------------------------------------------------------------------
----------------------------------------------------------------------------
 Explanation (enabled by `-explain`)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 You have specified more parameter lists than defined in the method definition(s).
----------------------------------------------------------------------------

可行修改方案

目前找到两种可编译的修改方式:

  • 方案A:在Part C中将println(x.fun())替换为println((x: A).fun())
  • 方案B:移除Part A中扩展方法的两个inline修饰符

疑问

但以上方案都不符合预期,想咨询:该问题的根源是什么?这是编译器bug,还是inline特性的已文档化限制?已测试过的Scala版本包括3.4.0-RC1-bin-20240104-2746ee8-NIGHTLY、3.4.0-RC1和3.3.1。


内容的提问来源于stack exchange,提问作者Koosha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 14:22:08