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
Прошу извинить, за редактуру =)
no subject
Славные были времена, в HP сидели ученые и что-то такое писали философское.
no subject
no subject
no subject
no subject
no subject
no subject
f a = x
f b = y
f c = y
(CoPowSet f) {} = {}
(CoPowSet f) {x} = {a}
(CoPowSet f) {y} = {b,c}
(CoPowSet f) {x,y} = {a,b,c}
Мы это вычислять не умеем.
no subject
no subject
no subject
Хаскельную форму функториальности я не затрагиваю, там категория другая. В скале категория типов и подтипов (ну ооп же). Нет смысла, имхо, вводить функторы, если они не работают на основной категории (подтипы).
no subject
no subject
() :;
(f :) :;
(z:, f : (, ) ) :;
(f :) :;
(y :) : = ! (filter (b y == b)).empty();
}
no subject
Тут такая штука, что это как бы недостаточно абстрактно. Если мы задаём множество по предикату, то ходить собирать всех, что равны... как-то смысла нету. Но идея всё равно понравилась, надо переварить, спасибо.
no subject
ООП иногда бывает очень странным.
no subject
no subject
Если множество задано предикатом, то оно вроде бы как контравариантно, потому что там ничего кроме этого предиката и нету, так?
no subject
Я использую 2.8, но множество определяю сам (а то слишком резво оно начинает вычисляться). И попробовал побаловаться с вариантностью вот.
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject