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

Compiler should detect if there are multiple implicit parameters with the same type #11087

Open
randomcoder opened this issue Aug 17, 2018 · 0 comments

Comments

@randomcoder
Copy link

The following snipped demonstrates a situation where it is possible to create a method with multiple implicit parameters of the same type (in this case Int). This is a non-sensical situation as the point of having two variables would be to have two different values and the implicit resolution will force only a single implicit value to be usable as you would require ambiguous implicit values to try and have a different value for each implicit parameter.

It may be good if the compiler would at least warn if this happens to highlight the likely coding error.

// Method defined with two implicit parameters of type Int
def withImplicitInts(in: String)(implicit i: Int, j: Int): String = {
  s"$in-$i-$j"
}

// Specifying implicit parameters directly allows for different values to be used
val i1 = withImplicitInts("s1")(10, 20) // val1: "s1-10-20"

// Define a single implicit val (type Int) The same implicit is used for both i and j
val i2 = {
  implicit val theI = 67
  withImplicitInts("s2")
} // val2: "s2-67-67"

// This fails to compile due to ambiguous implicit values as expected
val i3 = {
  implicit val theI = 9
  implicit val theJ = 87
  withImplicitInts("s3")
}

This was run with Scala 2.12.6 with Oracle Java 1.8.0_161_b12

@hrhino hrhino changed the title Compiler should detect if there are multiple implicit parameters with the same Compiler should detect if there are multiple implicit parameters with the same type Aug 17, 2018
@hrhino hrhino added this to the Backlog milestone Aug 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants