다음 예제는 'Programming in Scala'책의 예제입니다. 클래스를 감안할 때 '하여 Rational'다음과 같은 방법 정의 :스칼라 : 메서드 연산자 오버로드
def add(that: Rational): Rational =
new Rational(
this.numer * that.denom + that.numer * this.denom,
this.denom * that.denom
)
나는 성공적으로 지능 인수를 취하는 편의 버전으로 add 메소드를 오버로드 할 수 있으며, 는 위의 정의를 사용한다 :
def add(that: Int): Rational =
add(new Rational(that, 1))
지금까지 문제가 없습니다.
지금, 나는 운영자 스타일의 이름으로 메소드 이름을 변경하는 경우 : 그래서 같은
def +(that: Rational): Rational =
new Rational(
this.numer * that.denom + that.numer * this.denom,
this.denom * that.denom
)
그리고 과부하 :
(fragment of Rational.scala):19: error: value unary_+ is not a member of this.Rational
+(new Rational(that, 1))
^
:
def +(that: Int): Rational =
+(new Rational(that, 1))
나는 다음과 같은 컴파일 오류
컴파일러가 +
메서드의 단항 버전을 찾는 이유는 무엇입니까? 스칼라