Alternative to extending implicit classes #12807
qwbarch
started this conversation in
General Discussion
Replies: 1 comment 1 reply
-
So extension methods are made available whenever their containing object is available in implicit scope, so what you can do here is wrap the extension methods in some given object, e.g. trait Foo[F[_]]
trait Bar[F[_]]
trait FooSyntax:
given FooOps[F[_]]: Bar[F] with
extension (foo: Foo[F])
def helloWorld = "Hello, World!"
object foo extends FooSyntax
@main def Test =
import foo.given // import givens from foo
val fOpt = new Foo[Option] {}
println(fOpt.helloWorld) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In Scala 2, I could define an extension method using the following:
I could then import
foo
to make use ofhelloWorld
.In Scala 3, I'm trying to change the same thing to an extension method.
The problem is I can't extend the
Bar
trait like I could using an implicit class.This also does not work:
How can I model this using Scala 3's extension methods?
Beta Was this translation helpful? Give feedback.
All reactions