August 2017
Intermediate to advanced
440 pages
10h
English
Use-site variance and declaration-site variance basically describes the place in the code (site) where the variance modifier is specified. Let's consider a View and Presenter example:
interface BaseView interface ProductView : BaseView class Presenter<T> // Usage var preseter = Presenter<BaseView>() var productPresenter = Presenter<ProductView>() preseter = productPresenter // Error: Type mismatch // Required: Presenter<BaseView> // Found: Presenter<ProductView>
The Presenter class is invariant on its parameterT type. To fix the problem, we can explicitly define the subtyping relationship. We can do it in two ways (use-site and declaration-site). First, let's define the variance at the use-site: ...
Read now
Unlock full access