Search code examples
scalacompiler-errorsboundstype-parameter

How to use scala bounds of type parameter to access a method


I have the following definition of a class:

class Pipe[ A ]( a: A ) {
  def |>[ B ]( f: A => B ) = f( a )
  def map[A, B, C](f: C => B)(implicit ev: A =:= List[C]): Seq[B] = { a.map(f) }
}

The class above does not compile with the following error in the map method:

value map is not a member of type parameter A

I have tried two approaches but none work. How can I define a map method so that the a: A is know to be a sequence and therefore can use the map method?

TIA.


Solution

  • You are shadowing the type parameter A. Remove it from your map definition:

    class Pipe[ A ]( a: A ) {
        def |>[ B ]( f: A => B ) = f( a )
        def map[B, C](f: B => C)(implicit ev: A =:= List[B]): Seq[C] = a.map(f)
    }