Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method unary_~ in class must be called with () argument #17325

Closed
cacapouh opened this issue Apr 21, 2023 · 5 comments
Closed

Method unary_~ in class must be called with () argument #17325

cacapouh opened this issue Apr 21, 2023 · 5 comments
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@cacapouh
Copy link

cacapouh commented Apr 21, 2023

Compiler version

3.2.2

Minimized code

@main
def main() = {
  class DoubleOps(d: Double) {
    def unary_~(): Double = scala.math.ceil(d)
  }
  implicit def doubleOps(d: Double): DoubleOps = new DoubleOps(
    d
  )

  println {
    ~(0.5) // compilation error
  }
}

Output

sbt:scala3playground> compile;
[info] compiling 1 Scala source to /Users/tobita_yoshiki/work/tmp/scala3playground/target/scala-3.2.2/classes ...
[error] -- [E100] Syntax Error: /Users/tobita_yoshiki/work/tmp/scala3playground/src/main/scala/Main.scala:11:4 
[error] 11 |    ~(0.5)
[error]    |    ^^^^^^
[error]    |    method unary_~ in class DoubleOps must be called with () argument
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[warn] there was 1 feature warning; re-run with -feature for details
[warn] one warning found
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 0 s, completed 2023/04/21 23:43:07

Expectation

The compilation is successful and the following output is expected to be printed to standard output:

1.0

It has been confirmed to work with Scala 2.13.10.

// Scala 2.13.10
object Main extends App {
  class DoubleOps(d: Double) {
    def unary_~(): Double = scala.math.ceil(d)
  }

  implicit def doubleOps(d: Double): DoubleOps = new DoubleOps(
    d
  )

  println {
    ~(0.5) // 1.0
  }
}
@cacapouh cacapouh added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 21, 2023
@sjrd
Copy link
Member

sjrd commented Apr 21, 2023

unary_~ must be declared as

def unary_~ : Double = ...

without ().

@cacapouh
Copy link
Author

I see, it seems it was not a bug after all. I will close this issue.

@He-Pin
Copy link

He-Pin commented Apr 21, 2023

But the error message can be improved

@cacapouh
Copy link
Author

But the error message can be improved

I will create an Issue to improve the error message.

@som-snytt
Copy link
Contributor

It has been confirmed to work with Scala 2.13.10.

This is a "lie". It is deprecated and errors under -Xsource:3.

➜  scala -Xsource:3
Welcome to Scala 2.13.10 (OpenJDK 64-Bit Server VM, Java 19).
Type in expressions for evaluation. Or try :help.

scala>   class DoubleOps(d: Double) {
     |     def unary_~(): Double = scala.math.ceil(d)
           def unary_~(): Double = scala.math.ceil(d)
               ^
On line 2: error: unary prefix operator definition with empty parameter list is unsupported: instead, remove () to declare as `def unary_~ : Double = scala.math.ceil(d)`

The "real problem" for Scala 3 is that the warning is not reported because of the error.

This is an old "philosophical" issue, that first you resolve the errors and then you resolve the warnings.

Of course, the compiler ought to emit no warnings. The compiler should emit errors, and the linter or IDE should emit warnings (or "suggestions") that might help resolve the errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

No branches or pull requests

4 participants