简体   繁体   English

IntelliJ 无法识别 Scala 中的 TypeTag

[英]IntelliJ doesn't recognize TypeTag in Scala

I'm trying to compile these 2 lines of code in Scala (using Gradle):我正在尝试在Scala 中编译这两行代码(使用 Gradle):

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

It turns out somehow that IntelliJ mark the word runtime with the color red.事实证明,IntelliJ 以某种方式用红色标记了runtime这个词。 and throw me this error: object runtime is not a member of package reflect one error found并向我抛出此错误: object runtime is not a member of package reflect one error found

Now, the weird thing for me, is when I'm using the Scala REPL, it finds this package without any issue .现在,对我来说奇怪的是,当我使用 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's build.gradle file 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'
}

Versions版本

  • IntelliJ IDEA - Community - Version 2020.2.3 IntelliJ IDEA - 社区- 版本 2020.2.3
  • JDK - Version 14.0.1 JDK - 版本 14.0.1
  • macOS Catalina - Version 10.15.7 macOS Catalina - 版本 10.15.7
  • Scala - Version 2.13.3 Scala - 版本 2.13.3
  • Gradle - Version 6.7 Gradle - 版本 6.7

According to your build.gradle you didn't add scala-reflect dependency根据你的build.gradle你没有添加scala-reflect依赖

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

scala-library is not enough if you want to use Scala reflection.如果你想使用 Scala 反射, scala-library是不够的。

Try to add scala-reflect尝试添加scala-reflect

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM