I have a naive question about using type class in scala 3. So I defined a
Functor and an
Applicative trait in the standard way
trait Functor[F[_]]: extension[T] (f: F[T]) def fmap[S](g: T => S): F[S] trait Applicative[A[_]] extends Functor[A] : def pure[S](s: S): A[S] extension[T] (a: A[T]) infix def <*>[S](f: A[T => S]): A[S]
Then I am trying to add given instance that needs some other applicative:
given[A[_]: Applicative]: Applicative[[T] =>> Foo[A, T]] with override def pure[T](t: T): Foo[A, T] = Foo(pure(t)) // ... rest omitted
Foo accepts a
A[T]. But the above code resolves
pure(t) to the
pure being defined, rather than the
pure available from
A[_]: Applicative. How can I provide a hint so resolution goes to the right function?
BTW, I wish I could use
map instead of
Functor. But doing that would prevent me from calling the standard
map method if I want to provide
given instances for, say
List. Is there a way to work around this?
given[A[_]](using ev: Applicative[A]): Applicative[[T] =>> Foo[A, T]] with override def pure[T](t: T): Foo[A, T] = Foo(ev.pure(t))
BTW, I wish I could use map instead of fmap for Functor. But doing that would prevent me from calling the standard map method if I want to provide given instances for, say List. Is there a way to work around this?
extensioninside the typeclass definition.
stack trace is suppressed; run last Compile / dependencyBrowseTreeHTML for the full output
Athere will be a new different type
Bso in order to properly reference
Byou first need an instance of
I was trying to convince scala 3 to recusrively flatten nested
Eithers of some unknown depth but couldn’t figure out how to do it.
I got a match type that describes the expected output type:
case class Inv() type FlattenR[X] = Inv[X] match case Inv[Either[ValidationError, x]] => FlattenR[x] case Inv[x] => Either[ValidationError, x]
but couldn’t figure out a way around erasure to implement it at the value level?
implicit def ordered[A](implicit asComparable: AsComparable[A]): Ordering[A]