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
Hi,
I am trying to migrate a Scala 2 code to scala 3 that uses only matcher. It works fine in scala 2, but not in scala 3. Here is the scala 2 code that works:
val cities = List("Barcelona", "Hamburg")
val cities2 = List("Barcelona", "Hamburg")
cities ++ cities should contain only (cities2: _*)
However, this was giving an error in scala 3 with the usage of vararg(: _*) that it is not supported. So I tried with removing the vararg. It compiles but test fails as there are more elements. Is this method only cant be used in Scala 3 anymore?
val cities = List("Barcelona", "Hamburg")
val cities2 = List("Barcelona", "Hamburg")
cities ++ cities should contain only cities2
EDIT:
if I use cities ++ cities should contain only ("Barcelona", "Hamburg") then it works. So there is some problem with the vararg in Scala 3. I tried * and : _*, but both didn't work.
However, it works fine when I tried with a simple method that takes vararg:
def test(list: String*) = { println(list) }
val list = List("a", "b")
test(list:_*)
test(list*)
Thanks in advance
Yadu
The text was updated successfully, but these errors were encountered:
yadavan88
changed the title
only method not working in Scala 3 Matchers
only method not working in Scala 3 Matchers (as List)
Dec 8, 2023
@yadavan88 Sorry we somehow didn't notice your message, yes I think Scala 3 explicitly says the syntax no longer works:
[error] -- Error: /home/cheeseng/git/artima/scalatest/dotty/scalatest-test/target/scala-3/src_managed/test/org/scalatest/ListShouldContainOnlySpec.scala:143:45
[error] 143 | cities should contain only (cities2: _*)
[error] | ^
[error] | `_*` can be used only for last argument of method application.
[error] | It is no longer allowed in operands of infix operations.
I am not sure why we didn't have onlyElementsOf like allOf pairing with allElementsOf, @bvenners perhaps we should add that in?
For now perhaps the only workaround I can think of is:
Hi,
I am trying to migrate a Scala 2 code to scala 3 that uses
only
matcher. It works fine in scala 2, but not in scala 3. Here is the scala 2 code that works:However, this was giving an error in scala 3 with the usage of vararg(
: _*
) that it is not supported. So I tried with removing the vararg. It compiles but test fails as there are more elements. Is this methodonly
cant be used in Scala 3 anymore?EDIT:
if I use
cities ++ cities should contain only ("Barcelona", "Hamburg")
then it works. So there is some problem with the vararg in Scala 3. I tried*
and: _*
, but both didn't work.However, it works fine when I tried with a simple method that takes vararg:
Thanks in advance
Yadu
The text was updated successfully, but these errors were encountered: