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

only method not working in Scala 3 Matchers (as List) #2303

Open
yadavan88 opened this issue Dec 8, 2023 · 1 comment
Open

only method not working in Scala 3 Matchers (as List) #2303

yadavan88 opened this issue Dec 8, 2023 · 1 comment

Comments

@yadavan88
Copy link

yadavan88 commented Dec 8, 2023

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

@yadavan88 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
@cheeseng
Copy link
Contributor

cheeseng commented Feb 2, 2024

@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:

(cities should contain).only(cities2: _*)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants