3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rubyのp()的なものがScalaにも欲しい

Last updated at Posted at 2014-04-05

http://d.hatena.ne.jp/tonfabeem/20131110/1384044294 を参考に。ちょっとだけ改変。

//Array以外は動くpメソッド
def p[T](t: T)(implicit mt: Manifest[T] = null) = {
	println("%s: %s".format(t, 
		if (mt == null) "<?>" 
		else mt.toString)
	)
}

として、

val l1 = List(1, 2, 3)
val l2 = List(1, "hoge", l1)
p(l2)

とかすると

List(1, hoge, List(1, 2, 3)): scala.collection.immutable.List[Any]

こんな風にコンソールに出て幸せになれる。

リンク元にはArrayの時の解決策も載ってるので、型情報を調べてArrayの時はそっちの処理にしようかと思ったら、実行時に型情報取るのむちゃくちゃ大変っぽいので断念。

2.10以降はtagTypeで取るらしいけど最初から処理系にこれとっといてね、って指示しないと行けないのでutilがそれを前提にするのは辛いかなと。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?