question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Matching an AnyVal with private constructor

See original GitHub issue

I’m not sure if it’s limited to AnyVals or applies to any class with a private constructor, but it seems to be impossible to match arguments of such type.

Pseudo-example:

final case class CountryCode private (value: String) extends AnyVal
object CountryCode extends LazyLogging {
  def apply(code: String): Either[String, CountryCode] = { ... }
  }
}

foo.bar(any[CountryCode]).returns(baz)

fails compilations with

constructor CountryCode in class CountryCode cannot be accessed in <$anon: org.mockito.matchers.DefaultValueProvider[CountryCode]> from <$anon: org.mockito.matchers.DefaultValueProvider[CountryCode]>
        .bar(any[CountryCode])

I haven’t found a workaround for now

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
bbonannocommented, Jul 26, 2021

Scala version? (iirc on each version I use a different ~hack~ impl 😂 )

0reactions
Krevercommented, Jul 29, 2021

Right! I haven’t thought of that 😃 Thanks.

You can keep this issue open if you’d like to improve the error message, otherwise, feel free to close.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does having a private constructor on a value class negate the ...
A case class extending AnyVal must be a top-level class; i.e. cannot be nested within the scope of another class, trait, or object....
Read more >
Scala - AnyVal meets Pattern Matching - LinkedIn
class Month(private val n:Int) extends AnyVal{ override def toString: ... in pattern matching based on the parameters to the constructor, ...
Read more >
Value Classes and Universal Traits - Scala Documentation
0, value classes are a mechanism in Scala to avoid allocating runtime objects. This is accomplished through the definition of new AnyVal subclasses....
Read more >
[Solved]-Does having a private constructor on a value class negate ...
A case class extending AnyVal must be a top-level class; i.e. cannot be nested within the scope of another class, trait, or object....
Read more >
AnyVal - Scala 3 - EPFL
AnyVal is the root class of all value types, which describe values not implemented as objects in the ... cannot be used in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found