All Questions
Tagged with scala pattern-matching
1,273
questions
2
votes
1
answer
57
views
How to extract the matching object with 1 operation in Scala?
I have the following piece of code:
foo collectFirst {
case x if check(expensive(x)) => do_something(expensive(x))
case x if check2(expensive2(x)) => do_something_else(expensive2(x))
}
To ...
0
votes
1
answer
67
views
Misleading unreachable warning when a type pattern match that is the result of a inline expansion is involved?
The following method works fine when T and the types of all its components are specific types (contrary to abstract type parameters).
import scala.compiletime.*
private inline def loop[T <: Tuple, ...
0
votes
1
answer
57
views
When Option pattern matching optimizes up to if statements in Scala?
Here is the ifA function for Option, written using if-statements:
def ifA[A](fcond: Option[Boolean])(ifTrue: Option[A], ifFalse: Option[A]): Option[A] =
if (fcond.isDefined) {
if (fcond.get) ...
0
votes
1
answer
48
views
How to apply pattern match when needed to match a middle element of a Seq() in scala
This is kind of a learning question rather than a programming code, I have a Sequence as below,
val inputSeq = Seq("A","B","C","D","E")
so I want to ...
0
votes
1
answer
45
views
How to remove pattern matching for verifying user information in scala?
I am new to Scala as have been java developer until now.
I have a functionality to login like where user can have multiple passwords:
def login(User user) : Boolean = {
String username = user....
1
vote
1
answer
40
views
Scala, how to simplify or reuse side-effecting pattern matching logic?
I am looking for a way to refactor this code and make it cleaner. I am certain that there is a way but haven't been able to figure it out.
I am working with avro4s, I need to enable serialisation for ...
0
votes
0
answers
51
views
Tightened type checking rules for pattern bindings in Scala 3.2+
From Scala 3.2 and on, this pattern binding results in a compiler warning:
val tuples = List((1, 2), (3, 4))
val head :: tail = tuples
The warning reads:
pattern's type ::[(Int, Int)] is more ...
0
votes
1
answer
37
views
A proper way to identify a genericly-typed contents on an Any collection in Scala
first of all, I know many resources that explain the issue with type erasure, how to use ClassTags, TypeTags, etc... Yet, in this case I would kindly ask to focus on an exact problem, and suggest the ...
0
votes
1
answer
80
views
Scala nested match pattern matching
I'm writing a sort of rules engine that traverses a tree and establishes a context for each sub-tree and then uses match conditions to validate the tree and infer information about the nodes.
I had ...
2
votes
2
answers
74
views
How can I return a subclass of a type from a method with a generic, bounded type?
Given the below example, how can I make the code compile without resorting to casting in myMethod?
object Test {
sealed trait MyTrait
case class A(v: Int) extends MyTrait
case class B(v: Int) ...
1
vote
0
answers
88
views
Scala's equivalent to Haskell's as-patterns in pattern matching?
I have pattern matching in Scala with a long case of which I use a large part. Can I name the part of the case so I don't have to rewrite the whole expression?
Example:
x match {
case (("def&...
2
votes
1
answer
398
views
Make Compile Fail on Non-Exhaustive Match in Scala 3
Since Scala 2.13, -Wconf compiler flag allows to precisely control over which warnings should be handled as errors. The configuration string for -Wconf however is not always the same when migrating to ...
1
vote
1
answer
61
views
Scala pattern match case class instance with arbitrary number of None fields
Let's say I have the following snippet of Scala
case class A(a: Option[String], b: Option[String])
val v: A = A(None, None)
val vOp: Option[A] = v match {
case A(None, None) => None // can I ...
1
vote
1
answer
179
views
Scala type pattern matching
I want to implement a pattern matching over types of case class.
Caurrently I have this idea in mind:
val T = typeOf[Int]
val S = typeOf[String]
// other primitive types
val O = typeOf[Option[_]] // ...
0
votes
0
answers
295
views
Kotlin Array/List extractor in Pattern Matching
In Scala one can extract array elements in a pattern matching statement. For example, I want to take the first two elements of a string input:
private def parseFieldSize(s: String): Option[(Int, Int)...