Declaration-site variance was my internship project on the Dart team, and I’ve documented my personal experience on the team in the article Life as a Dart intern. As the primary implementer of the declaration-site variance feature, I want to share the usages and benefits of sound variance.

We’ll discuss how to use variance, why we want to use the modifiers, how the feature can build on top of classes that don’t use the modifiers, and what benefits this feature provides us.

Note:_ The implementation of variance isn’t finished yet. Although you can play with it by enabling the experiment (instructions below), the feature might change before it’s finalized._

Before diving into Dart’s declaration-site variance feature, we’ll take a quick detour to discuss what variance means and how it’s used.

What is variance?

To briefly introduce variance, we can look at this example:

main() {
	  Iterable<Object> objectIterable = <int>[1, 2, 3];
	}

From this, an Iterable of integers can be substituted as an Iterable of Objects because an integer is an Object and can be used in an Iterable in every place an Object would be.

#variance #dartlang #programming

Dart declaration-site variance
2.65 GEEK