f-bounded-polymorphism

scala f-bounded types explanation

时光毁灭记忆、已成空白 提交于 2019-11-27 14:50:10
After going through a few examples, I have to say, I fail to understand what the F-Bounded polymorphic brings. To use the example from scala school ( https://twitter.github.io/scala_school/advanced-types.html#fbounded ) They explain that they need some F-Bounded type so that the subclass can return the subtype. So they do something like this: trait Container[A <: Container[A]] extends Ordered[A] class MyContainer extends Container[MyContainer] { def compare(that: MyContainer) = 0 } But I don't see what is the gain of using this kind of type when you could use something like this: trait

scala f-bounded types explanation

限于喜欢 提交于 2019-11-26 17:49:29
问题 After going through a few examples, I have to say, I fail to understand what the F-Bounded polymorphic brings. To use the example from scala school (https://twitter.github.io/scala_school/advanced-types.html#fbounded) They explain that they need some F-Bounded type so that the subclass can return the subtype. So they do something like this: trait Container[A <: Container[A]] extends Ordered[A] class MyContainer extends Container[MyContainer] { def compare(that: MyContainer) = 0 } But I don't

Practical Uses for the “Curiously Recurring Template Pattern”

本秂侑毒 提交于 2019-11-26 12:55:08
问题 What are some practical uses for the \"Curiously Recurring Template Pattern\"? The \"counted class\" example commonly shown just isn\'t a convincing example to me. 回答1: Simulated dynamic binding. Avoiding the cost of virtual function calls while retaining some of the hierarchical benefits is an enormous win for the subsystems where it can be done in the project I am currently working on. 回答2: It's also especially useful for mixins (by which I mean classes you inherit from to provide