Juan-Carlos Gandhi (
juan_gandhi) wrote2010-05-15 11:04 am
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
So, what I am thinking. Can we make Set functor contravariant in OOP? I mean, we have two problems here:
Seems like variance just does not work here.
I'll continue investigating the opportunities for covariance.
/** * 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:
- Java type erasure makes it impossible to actually check the element type;
- 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.
no subject
() :;
(f :) :;
(z:, f : (, ) ) :;
(f :) :;
(y :) : = ! (filter (b y == b)).empty();
}
no subject
Тут такая штука, что это как бы недостаточно абстрактно. Если мы задаём множество по предикату, то ходить собирать всех, что равны... как-то смысла нету. Но идея всё равно понравилась, надо переварить, спасибо.