Skip to main content

All Questions

Tagged with
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 ...
pathikrit's user avatar
  • 33.5k
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, ...
Readren's user avatar
  • 1,216
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) ...
Max Smirnov's user avatar
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 ...
Hima_93's user avatar
  • 51
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....
Thinker's user avatar
  • 6,872
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 ...
ticofab's user avatar
  • 7,685
0 votes
0 answers
50 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 ...
lkbaerenfaenger's user avatar
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 ...
Uko's user avatar
  • 13.3k
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 ...
David Regan's user avatar
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) ...
tjarvstrand's user avatar
1 vote
0 answers
87 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&...
matj1's user avatar
  • 168
2 votes
1 answer
397 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 ...
alessandro candolini's user avatar
1 vote
1 answer
59 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 ...
sahibeast's user avatar
  • 351
1 vote
1 answer
178 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[_]] // ...
Lyashko Kirill's user avatar
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)...
pumpump's user avatar
  • 382

15 30 50 per page
1
2 3 4 5
85