1

After ripping my hair out for several hours trying to get a more complex piece of code to compile, I simplified my confusion down to this simple issue.

I had thought that a: => A was a zero argument function returning A just like a: () => A or a: Function0[A]. Apparently, this is not the case at all, and the root of my confusion.

Curiously, I can't declare a local var/val of type => A, I can only use that type for function arguments.

In hindsight, I would guess that this a lazy evaluation notation, not an actual function? Is this true? That is really confusing to use the function arrow for a non-function.

  // Compiler Error: Expression of type A doesn't conform to expected type () => A
  def notFunction0_1[A](a: => A) { val b: Function0[A] = a }

  // Compiler Error: Expression of type A doesn't conform to expected type () => A
  def notFunction0_2[A](a: => A) { val b: () => A = a }

  // Compiles. Also this matches inferred type of simply A.
  def justPlainA[A](a: => A) { val b: A = a }

  // While I can declare function arguments of type `=> A`, I can't declare local variables of the same type.
  def noLocal[A](a: => A) { val b: => A = a }
clay
  • 18,138
  • 28
  • 107
  • 192

0 Answers0