Discord is now Scala’s main chat platform. Please join us at https://discord.com/invite/scala
Monad
)
@BalmungSan Hmm... So, the instance that will always be pass will be the monad instance? What if I have a generalized function that just needs a Functor
constraint, how will the compiler find that instance?
For example:
def someGeneralizedFunc [F[_]: Functor](fa: F[String]): F[Int] = fa.fmap(x => 3)
someGeneralizedFunc(List("yo")) // I don't think this will work with just a Monad instance for List
Monad
can form a Functor
map
in terms of flatMap
and pure
Monad[F] extends Functor[F]
, as such, it is a subtype.
Applicative
already extends Functor
?
map
on a tuple?[error] | Found: Object with PolyFunction {...}
[error] | Required: PolyFunction{apply: [t](x$1: t): Any}
summonForT[A].map(
[r] =>
(reader: r) =>
inline reader match {
case reader: ConfigReader[t] =>
val key = fieldMapping("field")
reader.isInstanceOf[ReadsMissingKeys] match {
case true => reader.from(objCur.atKeyOrUndefined(key))
case false => objCur.atKey(key).flatMap(reader.from(_))
}
}
)
where summonForT
is an inline method which returns a Tuple.Map[m.MirroredElemTypes, ConfigReader]
I can't figure out why this code wouldn't compile:
case class Wrapper[A <: Singleton](a: A)(using ValueOf[A])
def test[A <: Singleton](blabla: Wrapper[A]): Unit = ()
test(Wrapper("yo"))
and fail with a:
No singleton value available for A
where: A is a type variable with constraint >: ("yo" : String) and <: Singleton
Wrapper("yo")
then it works:case class Wrapper[A <: Singleton](a: A)(using ValueOf[A])
def test[A <: Singleton](blabla: Wrapper[A]): Unit = ()
val value = Wrapper("yo")
test(value)
Hi :wave: I have a question about quotes and splices. In scala2 I was able to create an Expr[CC]
for a case class CC(...)
which was equivalent to instantiating the class CC.apply(arguments:_*)
as
val symbol = weakTypeOf[CC].typeSymbol
val constructor = q"reify(${c.mirror.staticModule(symbol.fullName)}).tree"
q"Apply($constructor, List(..$arguments))"
The thing which I am struggling with is that in scala2 I could interpolate instances of Tree
into a quasiquote and reify
and use quotes without getting staging level errors.
Please, anyone has a quick hint on how to replicate the three lines of scala2 in scala3? 🙏🏻
"yo"
to be String
, not "yo"
.
A
is indeed a singleton
"yo"
(as manually specifying that type makes it work). But I do mean to recall there has been such a problem mentioned somewhere (some issue on the tracker?) where one had to store things in a val
and pass that val
to the method for it to type it correctly.
A
in Wrapper
is somehow being "altered" by test
, which makes summoning ValueOf[A]
fails. What seems to also work is moving the using
-clause to test
, so it obviously is a case of the test
-method making the parameter less specific.
A
within test
doesn't resolve to "yo"
, I guess it resolves to String & Singleton
instead.
ValueOf
for it.
Singleton
Please, anyone has a quick hint on how to replicate the three lines of scala2 in scala3? 🙏🏻
I will try to give a more specific example. I would like to have an Expr[CC]
which is equivalent to instantiating a new instance of CC
, yet generic.
case class CC(i:Int)
val expected = '{ new CC(1) }
Attempts to generalize that:
val ts = TypeRepr.of[CC] // or Type[CC] ...
val attempt1 = '{new ts(1)} // something as simple as this fails
val attempt2 = Apply(
Select.unique(New(Ident(TermRef(ts,"???"))),"<init>"),
List(Literal(IntConstant(1)))
).asExprOf[CC] // I have no idea how to instantiate this
List[Expr[_]]
)Type[CC]
)def findGapsForSorted(x: List[Int]): List[Tuple2[Int, Int]] =
def solveFor(xs: List[Int]): List[Tuple2[Int, Int]] =
val midpoint = xs.length/2
if xs.length > 2 then
val first = xs.head
val last = xs.last
if (first + xs.length - 1 >= last)
then List((first, last))
else
solveFor(xs.slice(0, midpoint)) ++
solveFor(xs.slice(midpoint, xs.length))
else
xs match
case Nil => List.empty
case List(one) => List((one, one))
case start :: end :: Nil =>
if (start + 1) >= end
then List((start, end))
else List((start, start), (end, end))
case _ => ???
solveFor(x)
.foldLeft(List.empty) { (state, el) =>
state match
case h :+ (start, end) if end + 1 == el._1 => h :+ (start, el._2)
case xs => xs :+ el
}
.length
, ++
, :+
) that might make this routine slow for large inputs.
Hello there!
Is there some way to get at an object's runtime TypeTag
in Scala 3?
(I'm not using macros. I just want to output it, in order to provide a helpful message in some unit tests.)
Is it safe to add the 2.13 version of the scala-reflect
API to my libraryDependencies
with cross-compilation?
Or is there a better way of doing this in Scala 3?