gpt4 book ai didi

scala - Scala 中的 var_dump()

转载 作者:行者123 更新时间:2023-12-01 19:17:00 25 4
gpt4 key购买 nike

有没有方便的方法来转储Scala中指定对象的所有成员,
var_dump(), PHP function

最佳答案

正如“How to Dump/Inspect Object or Variable in Java”中提到的(是的,我知道,问题是关于Scala的):

Scala (console) has a very useful feature to inspect or dump variables / object values :

scala> def b = Map("name" -> "Yudha", "age" -> 27)
b: scala.collection.immutable.Map[java.lang.String,Any]

scala> b
res1: scala.collection.immutable.Map[java.lang.String,Any] = Map((name,Yudha), (age,27))

但是如果您想了解更多详细信息,您可以给 REPL Scala Utils尝试一下,才能得到“Easier object inspection in the Scala REPL

So I've written a utility for use on the Scala REPL that will print out all of the "attributes" of an object.

(注意:“我”在这里:Erik Engbrecht,也on BitBucket)

Here's some sample usage:

scala> import replutils._
import replutils._

scala> case class Test(a: CharSequence, b: Int)
defined class Test

scala> val t = Test("hello", 1)
t: Test = Test(hello,1)

scala> printAttrValues(t)
hashCode: int = -229308731
b: int = 1
a: CharSequence (String) = hello
productArity: int = 2
getClass: Class = class line0$object$$iw$$iw$Test

That looks fairly anti-climatic, but after spending hours typing objName to see what's there, and poking at methods, it seems like a miracle.
Also, one neat feature of it is that if the class of the object returned is different from the class declared on the method, it prints both the declared class and the actual returned class.

关于scala - Scala 中的 var_dump(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4736174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com