Aladdin - Scala Bugtracking
[#603] project: compiler priority: low category: bug
submitter assigned to status date submitted
Nikolay Martin fixed 2006-05-18 17:34:46.0
subject [contrib #139] incorrect code for lambda expression of type () => Any
code
object lazy {
  class Susp[+A](lazyValue: => A) extends Function0[A] {
    private var func: () => Any = () => lazyValue /* : A */
    private var value: Any = null

    override def apply() = {
      if (func != null) {
        value = func().asInstanceOf[A]
        func = null
      }
      value.asInstanceOf[A]
    }

    override def toString() =
      if (func == null) "Susp(" + value + ")"
      else "Susp(?)"
  }

  def delay[A](value: => A) = new Susp[A](value)
  implicit def force[A](s: Susp[A]): A = s()
}

object lazy_test {
  import lazy._
  
  def main(args: Array[String]) = {
    val s: Susp[Int] = delay { Console.println("evaluating..."); 3 }
    Console.println("s = " + s)
    Console.println("s() = " + s())
    Console.println("s = " + s)
    Console.println("2 + s = " + (2 + s))
  }
}
what happened
I get this output when I run lazy_test:

s = Susp(?)
s() = 
s = Susp()
Exception in thread "main" java.lang.ClassCastException: lazy_test$$anonfun$1
        at lazy_test$$anonfun$2.apply(lazy.scala:52)
        at lazy_test$$anonfun$2.apply(lazy.scala:52)
        at lazy_test$.main(lazy.scala:52)
        at lazy_test.main(lazy.scala)
what expected This is the correct output. It works when the type annotation in the comment on line 3 is added. s = Susp(?) evaluating... s() = 3 s = Susp(3) 2 + s = 5
[back to overview]
Changes of this bug report
Nikolay  edited on  2006-05-18 17:35:39.0
Martin  edited on  2006-05-19 15:24:30.0