Typing of
? Encapsulation (Solved: “existential types” [Girard 71])
? Inheritance (Partially solved)
?
Polymorphism (Open issue) Typing Inheritance
1.
Covariant Typing
Animal {
Shit eats(Food)
}
Cow extends Animal {
CowShit eats(Grass)
}
Problem: Not type sound
Animal a = new Cow();
Food f = new Hamburger();
a.eat(f); //Food poisoning
2.
Contravariant Typing
Animal {
Shit eats(Grass)
}
Cow extends Animal {
CowShit eats(Food)
}
Problem:
Sound but not flexible
3.
Novariant Typing
Animal {
Shit eats(Food)
}
Cow extends Animal {
Shit eats(Food)
}
Problem:
Worst of both worlds.
博客围绕继承的类型问题展开,涉及封装、继承和多态等方面。着重讨论了继承的类型,包括协变类型、逆变类型和不变类型,分析了每种类型存在的问题,如协变类型存在类型不安全问题,逆变类型缺乏灵活性,不变类型则兼具两者缺点。
5万+

被折叠的 条评论
为什么被折叠?



