You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 IntdefwithImplicitInts(in: String)(impliciti: Int, j: Int):String= {
s"$in-$i-$j"
}
// Specifying implicit parameters directly allows for different values to be usedvali1= withImplicitInts("s1")(10, 20) // val1: "s1-10-20"// Define a single implicit val (type Int) The same implicit is used for both i and jvali2= {
implicitvaltheI=67
withImplicitInts("s2")
} // val2: "s2-67-67"// This fails to compile due to ambiguous implicit values as expectedvali3= {
implicitvaltheI=9implicitvaltheJ=87
withImplicitInts("s3")
}
This was run with Scala 2.12.6 with Oracle Java 1.8.0_161_b12
The text was updated successfully, but these errors were encountered:
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
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.
This was run with Scala 2.12.6 with Oracle Java 1.8.0_161_b12
The text was updated successfully, but these errors were encountered: