This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Is Array still a covariant functor?

2 replies
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.

Can this trait be implemented for Array for 2.8.0?

trait Functor[F[_]] {
def fmap[A, B](a: F[A], f: A => B): F[B]
}

object Functor {
def ArrayFunctor = new Functor[Array] {
def fmap[A, B](a: Array[A], f: A => B) = {
// error: could not find implicit value for evidence parameter
of type scala.reflect.ClassManifest[B]
// a map f toArray

error("Can this be done?")
}
}
}

David Hall 4
Joined: 2009-08-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Is Array still a covariant functor?

On Sat, Nov 28, 2009 at 7:04 PM, Tony Morris wrote:
> Can this trait be implemented for Array for 2.8.0?
>
> trait Functor[F[_]] {
>  def fmap[A, B](a: F[A], f: A => B): F[B]
> }
>
> object Functor {
>  def ArrayFunctor = new Functor[Array] {
>    def fmap[A, B](a: Array[A], f: A => B) =  {
>      // error: could not find implicit value for evidence parameter
> of type scala.reflect.ClassManifest[B]
>      // a map f toArray
>
>      error("Can this be done?")
>    }
>  }
> }

I believe the answer is no. You need to say B:ClassManifest (which
breaks the interface) or you can use GenericArray

http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/...

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Is Array still a covariant functor?

Thanks David, I will implement accordingly.

David Hall wrote:
> On Sat, Nov 28, 2009 at 7:04 PM, Tony Morris wrote:
>
>> Can this trait be implemented for Array for 2.8.0?
>>
>> trait Functor[F[_]] {
>> def fmap[A, B](a: F[A], f: A => B): F[B]
>> }
>>
>> object Functor {
>> def ArrayFunctor = new Functor[Array] {
>> def fmap[A, B](a: Array[A], f: A => B) = {
>> // error: could not find implicit value for evidence parameter
>> of type scala.reflect.ClassManifest[B]
>> // a map f toArray
>>
>> error("Can this be done?")
>> }
>> }
>> }
>>
>
> I believe the answer is no. You need to say B:ClassManifest (which
> breaks the interface) or you can use GenericArray
>
> http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/...
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland