Scala 中的 var_dump()

发布于 2024-10-13 10:54:40 字数 150 浏览 2 评论 0原文

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

Is there any convenient way to dump all members of a specified object in Scala,
like var_dump(), PHP function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薯片软お妹 2024-10-20 10:54:40

如“如何转储/检查对象或Java中的变量”(是的,我知道,问题是关于Scala的):

Scala(控制台)有一个非常有用的功能来检查或转储变量/对象值:

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 尝试一下,以获得“Scala REPL 中的对象检查更容易"

因此,我编写了一个在 Scala REPL 上使用的实用程序,它将打印出对象的所有“属性”

(注:“我”在这里:Erik Engbrecht,还有< a href="https://bitbucket.org/eengbrec" rel="noreferrer">BitBucket)

以下是一些示例用法:

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

这看起来相当反常,但在花了几个小时输入 objName 来查看其中的内容并研究方法之后,这似乎是一个奇迹。
此外,它的一个巧妙的功能是,如果返回的对象的类与方法上声明的类不同,它会打印声明的类和实际返回的类。

As mentioned in "How to Dump/Inspect Object or Variable in Java" (yes, I know, the question is about 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))

But if you want more details, you can give REPL Scala Utils a try, in order to get a "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.

(Note: "I" being here: Erik Engbrecht, also 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.

苏辞 2024-10-20 10:54:40

在编译的代码中,最好的方法通常是将您的类型声明为案例类,然后使用生成的 toString 方法。

任何其他子类 Product 都应该同样简单(目前只是元组)

如果做不到这一点,请编写自己的 toString 方法,它通常很简单......

In compiled code, the nicest way is usually just to declare your type as a case class, then use the generated toString method.

Anything else subclassing Product should be just as easy (currently just tuples)

Failing that, write your own toString method, it's usually trivial enough...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文