0

Scala 関数のリテラル形式を調べるために単体テストを作成しましたが、非常にわかりにくいことがわかりました。異なる構文の意味を理解するのを手伝ってくれませんか?

@Test def supplierLiteral: Unit = {
    object Taker {

        def takeFunctionLiteral(supplier: => Int): Unit = {
            println("taker takes")
            //              println(supplier.apply()) //can't compile
            println(supplier)
        }

        def takeExplicitFunction0(supplier: () => Int): Unit = {
            println("taker takes")
            println(supplier())
        }
    }

    val give5: () => Int = () => {
        println("giver gives")
        5
    }


    println(give5.isInstanceOf[Function0[_]])

    Taker.takeFunctionLiteral(give5) //can't compile, expected Int
    println()
    Taker.takeExplicitFunction0(give5)
}

println(suppiler.apply())の構文が正しくないのはなぜtakeFunctionLiteralですか? どちらも同等ではありませんか?違いは何ですか

supplier: () => Int

supplier: => Int

前もって感謝します。

4

1 に答える 1