Is it possible to propagate a type parameter of a type parameter to a parameterized class in Scala?

Suppose I have a trait Foo[A, B].

I want to define a class Barparameterized with Fooand reuse parameters Fooin methods Bar. Something like the one shown below (this snippet does not compile):

trait Bar[Foo[A, B]] {
  def doSmth[C](A => C): C
}

Is there a way to achieve something like this and still only have one type parameter in the declaration Bar?

ps it would be great if someone could suggest the correct terminology for the material described.

+4
source share
2 answers

Not sure, but maybe this is somewhere nearby where you want to go.

trait Foo[A,B]
trait Bar[A] { self: Foo[A,_] =>
  def doSmth[C](atoc: A => C): C
}

, .

trait Foo[A,B] {type FooA = A}
trait Bar { self: Foo[_,_] =>
  def doSmth[C](atoc: FooA => C): C
}

, a Foo , Bar.

+1

# .

trait M {
    type A
    type B
  }

  class F[X,Y] extends M {
    type A = X 
    type B = Y 
  }

  trait Bar[F] {
    def doSmth[C](x: M#A => M#B): C
  }
+1

Source: https://habr.com/ru/post/1649942/


All Articles