0

I would like to do something akin to

trait Copyable[T] {
  extension (o: T) def copy: T
}

trait A : Copyable { // Context bound is not legal here...
  def foo(): A = copy
}

or

trait A {
  this: (A: Copyable) => // Context bound is not legal here...
  def foo(): A = copy
}

In other words, I would like the trait A to "extend" Copyable, or only be mixable with Copyable types.

Note: Ideally, foo would have type this.static_type (doesn't currently exist), which would represent the most specific static type of this.

Stephane Bersier
  • 710
  • 7
  • 20
  • I would make `A` a typeclass too, but you may do this: `trait A[AA <: A[AA]](using Copyable[A]) {` but then again why make `Copyable` a typeclass to return to F-Bounded polymorphism. – Luis Miguel Mejía Suárez Feb 12 '22 at 18:07
  • @LuisMiguelMejíaSuárez Thanks. Yeah, I guess that's the best solution I'm going to get at this point. The code I wrote is just an example, probably not the best. Imagine you already have some type classes, and you have a good reason to write a new trait (rather than a type class), but you want to reuse functionality from your type classes. – Stephane Bersier Feb 12 '22 at 18:45
  • Yeah, you will probably need to use F-Bound. I just say that there is probably a reason why the original typeclasses are typeclasses instead of OOP interfaces. – Luis Miguel Mejía Suárez Feb 12 '22 at 19:38
  • This seems like it may be an [XY problem](https://en.wikipedia.org/wiki/XY_problem). If you have concrete implementations in your trait, perhaps you could make it into a group of extensions like [this](https://scastie.scala-lang.org/fDJOzyZNSXmSWQ6IFX3Wrg)? – user Feb 19 '22 at 19:07
  • @LuisMiguelMejíaSuárez Or they could add a context bound to every single method in the trait, although that'd get annoying pretty quick. – user Feb 19 '22 at 19:13
  • 1
    @user actually OP ported the question to the [**discord** server](https://discord.com/channels/632150470000902164/632150470000902166/942149206368481370), it seems all he / she wanted to know was how to use context bounds with traits and if the language may support a way to refer to the current class type in the future. – Luis Miguel Mejía Suárez Feb 19 '22 at 19:34
  • I actually asked a different (albeit related) question on the discord server. – Stephane Bersier Feb 20 '22 at 22:43

0 Answers0