juan_gandhi: (Default)
Juan-Carlos Gandhi ([personal profile] juan_gandhi) wrote2010-05-15 11:04 am
Entry tags:

let me show you something

I was experimenting with Set[T] functor variance. Here are my considerations regarding the opportunity to make it contravariant. In short: it's impossible.

    /**
     * Casts a set into a set of subtype (contravariance, that is)
     * This method does not work, due to JVM type erasure.
     * Actually, delegating the work to runtime would be a stupid idea anyway;
     * so, is it a general problem with oop? Have to think.
     */
    @Deprecated  
    /*implicit*/ def downshift[A, B >: A] (source: Set[B]) : Set[A] = {
      val b2a: (B => A) = {case (a: A) => a}
      val elements = {for (b <- source; if b.isInstanceOf[A]) yield b2a(b)}
      setOf(elements, source.size, source.contains _)
    }


So, what I am thinking. Can we make Set functor contravariant in OOP? I mean, we have two problems here:

  1. Java type erasure makes it impossible to actually check the element type;

  2. OOP in general forces us to delegate this type checking to runtime - which is, I assume, a wrong idea



Seems like variance just does not work here.

I'll continue investigating the opportunities for covariance.

[identity profile] cema.livejournal.com 2010-05-18 04:56 am (UTC)(link)
Clojure есть. Но я с ней, наверно, уже надоел. К тому же, она без классов практически.

[identity profile] alexott.livejournal.com 2010-05-18 06:27 pm (UTC)(link)
да она там вроде тоже не особо живая, по сравнению с JVM версией :-)