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] huzhepidarasa.livejournal.com 2010-05-16 07:39 am (UTC)(link)
Ага, в Скале же есть и lower bound, и upper bound. Можно сделать ковариантную проверку принадлежности:
   {
      () :;
       (f :) :;
       (z:, f : (, )  ) :;
      (f :) :;
       (y :) : = ! (filter (b  y == b)).empty();
}
Это оно или не оно? (Я пока со Скалой только играюсь, тонкостей не понимаю).

[identity profile] ivan-gandhi.livejournal.com 2010-05-16 03:28 pm (UTC)(link)
Это любопытно. Нет, это очень даже прикольно.

Тут такая штука, что это как бы недостаточно абстрактно. Если мы задаём множество по предикату, то ходить собирать всех, что равны... как-то смысла нету. Но идея всё равно понравилась, надо переварить, спасибо.