Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adaptation of argument list by inserting () has been deprecated

Tags:

scala

I'm just in the process of upgrading from Scala 2.10.x to 2.11.2 and I'm receiving the following warning with the following code:

  override def validateKey(key: String): Either[InvalidKeyError, Unit] = 
    keys.contains(key) match {
      case true => Right()
      case false => Left(InvalidKeyError(context, key))
    }

Adaptation of argument list by inserting () has been deprecated: this is unlikely to be what you want. signature: Right.apply[A, B](b: B): scala.util.Right[A,B] given arguments: after adaptation: Right((): Unit)

I am able to solve this by changing the "true" case statement to:

case true => Right(()) //() is a shortcut to a Unit instance

Is this the proper way to address this warning?

Edit: perhaps a "why we have to do this now" type answer would be appropriate, my cursory investigation seems to indicate that Scala inserting "Unit" when it thinks it needs to causes other problems

like image 548
ThaDon Avatar asked Aug 17 '14 17:08

ThaDon


3 Answers

Automatic Unit inference has been deprecated in scala 2.11, and the reason behind this is that it can lead to confusing behavior, especially for people learning the language.

Here's an example

class Foo[T](value: T)
val x = new Foo

This should not compile, right? You are calling the constructor with no arguments, where one is required. Surprisingly, until scala 2.10.4 this compiles just fine, with no errors or warnings.

And that's because the compiler inferred a Unit argument, so it actually replaced your code with

val x = new Foo[Unit](()) // Foo[Unit]

As the newly introduced warning message says, this is unlikely to be what you want.

Another famous example is this

scala> List(1,2,3).toSet()
// res1: Boolean = false

calling toSet() should be a compile-time error, since toSet does not take arguments, but the compiler desperately tries to make it compile, ultimately interpreting the code as

scala> List(1,2,3).toSet.apply(())

which means: test whether () belongs to the set. Since it's not the case, you get a false!

So, starting from scala 2.11, you have to be explicit if you want to pass () (aka Unit) as an argument. That's why you have to write:

Right(())

instead of

Right()

examples taken from Simplifying Scala — The Past, Present and Future by Simon Ochsenreither.

like image 90
Gabriele Petronella Avatar answered Nov 03 '22 06:11

Gabriele Petronella


Perhaps it should be Right(()). Have you tried that?

like image 41
Ashalynd Avatar answered Nov 03 '22 05:11

Ashalynd


My explanation is that since the Right.apply is polymorphic it can take all kind of parameters, doing Right() means passing in a Unit and the compiler simply advise you that maybe that's not what you want, he doesn't know that this is what you actually want.

If you see your deprecate message it states:

... after adaptation: Right((): Unit)

Which means that the compiler has automatically decided that you are passing in a Unit, since this is kinda like void he doesn't really likes it, specifically passing in a Unit like () explicitly tells the compiler that you do want a Unit there. Anyway seems a new deprecation form scala 2.11, I can't reproduce this on 2.10.4.

like image 4
Ende Neu Avatar answered Nov 03 '22 05:11

Ende Neu