Skip to main content

All Questions

Tagged with
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 ...
LoranceChen's user avatar
  • 2,541
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, ...
Amit Bashan's user avatar
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 ...
MiNoS's user avatar
  • 27
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. ...
MiNoS's user avatar
  • 27
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(...
RobertJo's user avatar
  • 125
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 ...
Readren's user avatar
  • 1,216
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, ...
Daniel's user avatar
  • 9,191
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 ...
lewistg's user avatar
  • 338
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 ...
Readren's user avatar
  • 1,216
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 ...
Jimmy Isaac's user avatar
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[...
avijitk's user avatar
  • 61
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 ...
Mamaf's user avatar
  • 360
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]...
Suma's user avatar
  • 34.1k
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,...
galbarm's user avatar
  • 2,533
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 ...
bobismijnnaam's user avatar

15 30 50 per page
1
2 3 4 5
133