메서드가 매개변수를 받지 않음

메서드가 매개변수를 받지 않음 (Method Does Not Take Parameters)

매개변수를 받지 않는 메서드나 표현식에 인자를 넘기려 할 때 나오는 에러예요.

출처: Scala 3 Reference

본문

매개변수를 받지 않는 메서드나 표현식에 인자를 전달하려고 하면 이 에러가 발생해요.

이런 상황에서 주로 나타나요.

  • 매개변수 목록이 없는 널러리(nullary) 메서드를 인자와 함께 호출한 경우
  • 메서드가 받을 수 있는 것보다 더 많은 인자 목록을 넘긴 경우
  • 함수가 아닌 표현식에 인자를 적용하려 한 경우

예시

object Hello
val message = Hello("World")

에러 메시지

-- [E050] Type Error: example.scala:2:14 ---------------------------------------
2 |val message = Hello("World")
  |              ^^^^^
  |              object Hello does not take parameters
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | You have specified more parameter lists than defined in the method definition(s).
   -----------------------------------------------------------------------------

해결 방법

// Enusre that method you want to call exists, especially if it involved Scala syntax desugaring
object Hello:
  def apply(value: String) = ???
val message = Hello("World")
// Enusre that that method actually takes arguments list
class Bag extends scala.reflect.Selectable

def example =
  val bag = new Bag:
    val f1 = 23

  val x = bag.f1 // instead of bag.f1()

더 알아보기

  • 호출하려는 메서드가 실제로 존재하는지 먼저 확인해 보세요. 특히 Scala 문법이 desugaring 되는 경우라면요.
  • object 같은 값에 괄호로 인자를 넘기려면 apply 메서드를 정의하면 됩니다.