加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

var_dump()在Scala中

发布时间:2020-12-16 09:11:39 所属栏目:安全 来源:网络整理
导读:有没有任何方便的方法来转储 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 d
有没有任何方便的方法来转储 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读