- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Better way to write this?
I feel like there is a slightly shorter way to write the foreach part, but I can't recall:
val list = 1 :: 2L :: 3d :: Nil
list.foreach { n => n match { case i: Int => println("Int: " + i) case l: Long => println("Long: " + l) case d: Double => println("Double: " + d) case _ => println("Unknown: " + n) }}
val list = 1 :: 2L :: 3d :: Nil
list.foreach { n => n match { case i: Int => println("Int: " + i) case l: Long => println("Long: " + l) case d: Double => println("Double: " + d) case _ => println("Unknown: " + n) }}










Re: Better way to write this?
You should be able to drop the match part:
list foreach { case i: Int => println("Int: " + i) case l: Long => println("Long: " + l) case d: Double => println("Double: " + d) case n => println("Unknown: " + n)}
--
Derek Williams
Re: Better way to write this?
On Fri, Jan 13, 2012 at 4:17 PM, Derek Williams <derek [at] fyrie [dot] net> wrote:
Re: Better way to write this?
Yes, of course. It felt wrong, and it was.
Thanks!