Why apply() can not be used in shorthand form on package objects?

后端 未结 1 331
猫巷女王i
猫巷女王i 2021-01-04 21:57

On normal objects, I can do the following:

object A {
  def apply = \"!\"
}
A() // \"!\"

But on package objects, this doesn\'t work:

<
1条回答
  •  太阳男子
    2021-01-04 22:26

    The only way you can do it without apply is this:

    A.`package`()
    

    This is because A does not denote a value or a method, and the language specification states that for f() to be valid, f has to have a method type or a value type with an apply method. I have no idea how easily one could "tweak" the compiler to change this, but I doubt it's worth the effort. If you do want to go to such lengths, it would be easier to just add your method to Predef.

    0 讨论(0)
提交回复
热议问题