简体   繁体   中英

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.

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM