简体   繁体   English

没有给出变量时,如何确定其类型?

[英]How can I determine the type of a variable when it is not given?

Assume, we have something like: 假设,我们有这样的东西:

val x = "foo".charAt(0)

and let us further assume, we do not know the return type of the method charAt(0) (which is, of course, described in the Scala API). 让我们进一步假设,我们不知道方法charAt(0)的返回类型(当然,这在Scala API中进行了描述)。 Is there a way, we can find out, which type the variable x has after its definition and when it is not declared explicitly? 有没有一种方法,我们可以找出变量x在定义之后有什么类型,什么时候没有明确声明呢?

UPDATE 1: My initial question was not precise enough: I would like to know (for debugging reasons) what type the variable has. 更新1:我最初的问题不够精确:我想(出于调试原因)想知道变量的类型。 Maybe there is some compiler option to see what type the variable get declared to by Scala's type inference ? 也许有一些编译器选项可以查看Scala的类型推断将变量声明为哪种类型?

Suppose you have the following in a source file named Something.scala : 假设您在名为Something.scala的源文件中具有以下内容:

object Something {
  val x = "foo".charAt(0)
}

You can use the -Xprint:typer compiler flag to see the program after the compiler's typer phase: 您可以使用-Xprint:typer编译器标志在编译器的typer阶段之后查看程序:

$ scalac -Xprint:typer Something.scala
[[syntax trees at end of typer]]// Scala source: Something.scala
package <empty> {
  final object Something extends java.lang.Object with ScalaObject {
    def this(): object Something = {
      Something.super.this();
      ()
    };
    private[this] val x: Char = "foo".charAt(0);
    <stable> <accessor> def x: Char = Something.this.x
  }
}

You could also use :type in the REPL: 您也可以在REPL中使用:type

scala> :type "foo".charAt(0)
Char

scala> :type "foo".charAt _
Int => Char

Your IDE may also provide a nicer way to get this information, as Luigi Plinge points out in a comment above. 正如Luigi Plinge在上面的评论中指出的那样,您的IDE可能还会提供一种更好的方式来获取此信息。

使用此方法解决问题:

x.getClass

Here's an easier version of Travis first alternative: 这是Travis第一个替代方案的简单版本:

dcs@dcs-132-CK-NF79:~/tmp$ scala -Xprint:typer -e '"foo".charAt(0)'
[[syntax trees at end of                     typer]] // scalacmd8174377981814677527.scala
package <empty> {
  object Main extends scala.AnyRef {
    def <init>(): Main.type = {
      Main.super.<init>();
      ()
    };
    def main(argv: Array[String]): Unit = {
      val args: Array[String] = argv;
      {
        final class $anon extends scala.AnyRef {
          def <init>(): anonymous class $anon = {
            $anon.super.<init>();
            ()
          };
          "foo".charAt(0)
        };
        {
          new $anon();
          ()
        }
      }
    }
  }
}

If you are using IntelliJIDEA , to show Type Info action in Editor, navigate to the value and press Alt + = for Windows and Ctrl + Shift + P for Mac: 如果使用的是IntelliJIDEA ,要在编辑器中显示“类型信息”操作,请导航至该值,然后对于Windows按Alt + = ,对于Mac Ctrl + Shift + P

在此处输入图片说明

I find it very handy when writing code. 我在编写代码时非常方便。

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

相关问题 如何确定 Akka 字节串是否包含给定的子串? - How can I determine if Akka bytestring contains a given substring? 如何获得与给定实例相同类型的空集合? - How can I get an empty collection of the same type as a given instance? 我可以在扩展给定类型时自动运行 Scala 宏吗? - Can I run a Scala macro automatically when I extend a given type? 当我调用 changeName 方法时,如何在 changeName 变量中覆盖 class 中给出的变量 studentName,并且在 userName 中使用新变量? - How to override variable studentName given in class within changeName variable and new variable is used in userName when I call changeName method? 在运行时确定或访问类型变量的具体类型 - Determine or access the concrete type of a type variable at runtime 给定一些路径依赖类型的值,我如何获得“容器”的实例? - Given a value of some path-dependent type, how can I get an instance of the “container”? 如何确定Scala中对象的类? - How can I determine the class of an object in Scala? 在Scala中初始化时如何强制数组的类型? - How can I force the type of an array when initialized in Scala? 处理集合时如何改变类型参数? - How can I vary the type parameter when dealing with a collection? Scala:如何确定类型是否可为空 - Scala: how to determine if a type is nullable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM