All Questions
1,994
questions
0
votes
1
answer
45
views
scala how to declare a type define which must have implicit conversion?
I need call a function def fromJsonString[Result <: B : C](jsonString: String): Result = ???.
And I define a trait MyContext in order to matching a various instance of type Result.
MyContext define ...
0
votes
0
answers
36
views
Implicit conversion of subtypes not recognized by Scala 3 compiler
I have the following code:
enum Value {
case Nil
case Boolean(value: scala.Boolean)
case Number(value: Double)
case String(value: String)
}
object Value {
given Conversion[scala.Boolean, ...
1
vote
1
answer
81
views
Scala 3 'cast' generic type to match type
Recently I started to port a simple ORM from Scala 2 to Scala 3 using generics and match types. After struggling with the type projection (see How to overcome Scala 3 missing type projection?) I now ...
1
vote
1
answer
99
views
How to overcome Scala 3 missing type projection?
In Scala 2 I used type projection in an simple ORM model which contains of IdEntity and some Repositories. Now I tried to migrate to Scala 3 but stuck since type projection with # is missing.
...
0
votes
1
answer
49
views
Using upickle read in Scala 3 macro
Try to write a generic macro for deserialising case classes using uPickle read in Scala 3:
inline def parseJson[T:Type](x: Expr[String])(using Quotes): Either[String, Expr[T]] = '{
try
Right(...
3
votes
1
answer
125
views
What is the difference between the types Null and Null & T where T <: AnyRef?
Look at the following method
def toOption[T <: AnyRef](value: T | Null): Option[T] =
value match {
case null => None
case content: T => Some(content)
}
If you ...
0
votes
2
answers
77
views
How to .copy on a generic case class without directly extending them?
How do I return a copy of a generic case class using the case class .copy method?
I'm trying to achieve the following:
def modifyNumber[T <: { def number: Int }](objectWithNumber: T, ...
0
votes
1
answer
50
views
Scala type mismatch error involving generics and refined types
The following is a simplified version of a type-mismatch error I'm trying to solve:
trait Foo[A] {
type B
val b: B
}
trait Bar[T, R[_]] {
val r: R[T]
}
object TypeMismatch extends App {
def ...
1
vote
1
answer
67
views
How to create an Array[T | Null] where T is a type parameter with `AnyRef` as upper bound
I need to create a temporary Array[T | Null] where T is a type parameter with AnyRef as upper bound.
Given the arrays implementation will use an Array[AnyRef] after erasure for whatever T is, why ...
0
votes
2
answers
50
views
Scala 3 generic method type parameter not working
Works:
trait Transformer[A, B] {
def transform(a: A): B
}
class TransformerImpl extends Transformer[Int, String] {
override def transform(value: Int): String = {
value.toString
}
}
This is ...
4
votes
1
answer
65
views
Incompatible pattern type when pattern matching a Class's generic parameter
Why does pattern matching on a Class's generic constructor parameter fail, while that on a method's generic parameter succeed?
I have a type class Ev:
sealed trait Ev[T]
case object EvInt extends Ev[...
1
vote
1
answer
88
views
Scala method with generic return type
I have a function which consists of parsing a json file to return a json defined configuration type:
import com.google.gson.Gson
def parseJsonFile(pathToFile: String): JsonConfig = {
var ...
3
votes
1
answer
72
views
How to implement a generic trait method in Scala 3 when the generic and concrete signatures match?
Following code compiles fine in Scala 2.13.12, but not in Scala 3.3.1:
trait Vectoric[V] {
def times(a: V, f: Double): V
def times(a: V, b: V): V
}
object DoubleIsVectoric extends Vectoric[Double]...
0
votes
2
answers
168
views
How to instantiate Scala 3 enum from generic type
Say I have a method in Scala 3 with the following signature:
def foo[E <: scala.reflect.Enum](str: String): E
How can I instantiate E according to str?
If for example, E is:
enum MyEnum:
case a1,...
2
votes
1
answer
132
views
Scala 3 generic tuples: type bound & converting to Seq
I have two problems, which I think are related.
I want to have a type for tuples, of which all elements of the tuple are at least an Animal, in Scala 3.3.1. I came up with the following:
// In ...