1

if when calling by-value

val f: (Int) => Int = {(i) => {i * i}} # f: Int => Int = <function1>
is the shorthand for
val f: Function1[Int, Int] = {(i) => {i * i}} # f: Int => Int = <function1>

then when calling by-name
val f: (=> Int) => Int = {(i) => {i * i}} # f: (=> Int) => Int = <function1>
is the shorthand for
? what ?

and if

when calling by-value

val f = {(i) => {i * i}}:(Int) => Int # f: Int => Int = <function1>
is the shorthand for
val f = {(i) => {i * i}}:Function1[Int, Int] # f: Int => Int = <function1>

then when calling by-name
val f = {(i) => {i * i}}:(=>Int) => Int # f: (=> Int) => Int = <function1>
is the shorthand for
? what ?

in other words

if (Int) => Int is shorthand for Function1[Int, Int]

then (=> Int) => Int is shorthand for ? what ?

Thank you !

Catalin Enache
  • 758
  • 1
  • 10
  • 17

2 Answers2

1

At the bytecode level, it's shorthand for :

Function1[Function0[Int], Int]

If you want to call such Scala code from another JVM language, you'll have to fullfill that signature.

See the source code for Function0, you won't find it in the scaladoc

Chirlo
  • 5,989
  • 1
  • 29
  • 45
  • Thanks Chirlo, but, it seems the signature for "Function1[Function0[Int], Int]" is "(() => Int) => Int = " and not "(=> Int) => Int = ". Scala does not allow me to declare this (not even compile): "val f: Function1[Function0[Int], Int] = {(i) => {i * i}}" as equivalent of val f: (=> Int) => Int = {(i) => {i * i}}. An acceptable declaration would be "val f: Function1[Function0[Int], Int] = {(i) => i()}" though it is not the 1 to 1 equivalence – Catalin Enache May 11 '15 at 12:27
  • 1
    I updated my answer, which is based on the bytecode scalac generates. – Chirlo May 11 '15 at 12:45
1

It isn't shorthand for anything. By-name types are by-name types. See SLS 4.6.1, http://www.scala-lang.org/files/archive/spec/2.11/04-basic-declarations-and-definitions.html#by-name-parameters.

It is true that if you look at the resulting bytecode, you will see that the argument will be passed as a Function0, but that's a bytecode-level implementation detail. At the language level, by-name types aren't just syntactic sugar. They are actual types (though they can only appear as parameter types, not in other contexts).

See also: Use of Scala by-name parameters

Community
  • 1
  • 1
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
  • Thanks Seth. I was afraid this would be the answer and frankly I don't like the fact. To me this is a language inconsistency. Anyways, it is good to know (at least for current Scala version 2.11.6) that no such equivalence exists. (I currently learn Scala trying to keep organized the stuff encountered, but in this case the pattern I was looking for didn't fit well) – Catalin Enache May 11 '15 at 12:58