CodeBlanks Logo Icon CodeBlanks

Summary - Language design

Combine null safety, type system and records to write a type system summary.

Summary: Language design

In this group we covered the pillars of Dart's language design: sound null safety catches null reference errors at compile time; the static type system ensures type safety.

Subtyping enables polymorphism; records and patterns group and destructure data lightly. Together they make Dart both safe and flexible. In this summary you will write code that combines all of these.

Practice Summary: Combine all concepts

Define a generic Wrapper<T> class with final T value; Wrapper(this.value); field and constructor. Define a function Wrapper<int> wrap(int? maybeAge) => Wrapper(maybeAge ?? 0); that wraps a nullable int?. In main create final w = wrap(null);; print the result with print(w.value);.

Starter code

main.dart
void main() {
  // define a Wrapper<T> generic class
  // define a wrap function (wraps a nullable age value)
  // call wrap with null; print the result
}

Sample solution

main.dart
class Wrapper<T> {
  final T value;
  Wrapper(this.value);
}

Wrapper<int> wrap(int? maybeAge) => Wrapper(maybeAge ?? 0);

void main() {
  final w = wrap(null);
  print(w.value);
}

Expected output

0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What do sound null safety and the type system provide together?

  • Shorter code
  • Faster runtime
  • Compile-time type and null safety Correct answer
  • Garbage collector removal

Explanation: sound null safety and static types together provide compile-time safety.

2 Match the design concepts

Term Meaning
Sound null safety Null reference safety
Type system Static type checking
Subtyping Polymorphism
Records Lightweight data grouping

3 Record destructuring

Question
final point = (3, 4);
final (int x, ____) = point;
print('$x,$y');
Answer
final point = (3, 4);
final (int x, int y) = point;
print('$x,$y');

Explanation: final (int x, int y) = point; destructures the record.

All lessons in Expert