【问题标题】:IntelliJ doesn't recognize TypeTag in ScalaIntelliJ 无法识别 Scala 中的 TypeTag
【发布时间】:2021-02-20 12:16:18
【问题描述】:

我正在尝试在 Scala 中编译这两行代码(使用 Gradle):

import import scala.reflect.runtime.universe._
val typeInt = typeTag[Int]

事实证明,IntelliJ 以某种方式将单词 runtime 标记为红色。并抛出这个错误:object runtime is not a member of package reflect one error found

现在,对我来说很奇怪的是,当我使用 Scala REPL 时,它发现这个包没有任何问题

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> val typeInt = typeTag[Int]
val typeInt: reflect.runtime.universe.TypeTag[Int] = TypeTag[Int]

Gradle 的 build.gradle 文件

plugins {
    id 'java-library'
    id 'scala'
    id 'com.github.maiflai.scalatest' version '0.26'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'org.scala-lang:scala-library:2.13.3'
    implementation 'com.fasterxml.jackson.module:jackson-module-scala_2.13:2.9.9'
    implementation 'com.typesafe:config:1.4.0'
    testCompile 'org.scalamock:scalamock_2.13:4.4.0'
    testCompile 'org.scalatest:scalatest_2.13:3.1.2'
    testRuntimeOnly 'com.vladsch.flexmark:flexmark-all:0.35.10'
}

版本

  • IntelliJ IDEA - 社区 - 版本 2020.2.3
  • JDK - 版本 14.0.1
  • ma​​cOS Catalina - 版本 10.15.7
  • Scala - 版本 2.13.3
  • Gradle - 6.7 版

【问题讨论】:

  • 第一个代码行中的双 import 是错字,对吧?
  • 来自 REPL 的反馈

标签: java scala gradle intellij-idea reflection


【解决方案1】:

根据您的build.gradle,您没有添加scala-reflect 依赖项

https://mvnrepository.com/artifact/org.scala-lang/scala-reflect/2.13.3

如果你想使用 Scala 反射,scala-library 是不够的。

尝试添加scala-reflect

implementation 'org.scala-lang:scala-reflect:2.13.3'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    相关资源
    最近更新 更多