Lazy initialiazation + Tuple extractors

89 views
Skip to first unread message

missingfaktor

unread,
Jun 23, 2012, 11:57:10 AM6/23/12
to scala-user
scala> lazy val (a, b) = (2, { println("hgello"); 4 })
a: Int = <lazy>
b: Int = <lazy>

scala> a
hgello
res9: Int = 2

scala> b
res10: Int = 4

Is the above an expected behavior?

I thought I was declaring two lazy values, but apparently, that's not what it does.
--
Cheers,

Dave

unread,
Jun 23, 2012, 12:06:43 PM6/23/12
to scala-user
I think the tuple itself is lazy

So when one of elements are called the whole tuple is evaluated.

scala> lazy val (a, b) = (2, { println("hello"); 4 })
a: Int = <lazy>
b: Int = <lazy>

scala> a
hello
res25: Int = 2

scala> lazy val (a, b) = (2, { println("hello"); 4 })
a: Int = <lazy>
b: Int = <lazy>

scala> b
hello
res26: Int = 4
> missingfaktor <http://twitter.com/#!/missingfaktor>.
Reply all
Reply to author
Forward
0 new messages