0Pricing
Dart Academy · Lesson

Extension Types for Zero-Cost Wrappers

Wrap a type without runtime overhead.

Extension Types for Zero-Cost Wrappers is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

A Different Tool

An extension type is a newer feature: it wraps an existing type to give it a fresh interface, with no runtime cost.

Wrap Without Boxing

Unlike a real wrapper class, an extension type adds no object at runtime. At runtime it simply is the underlying value.

extension type UserId(int id) {}

The Representation Type

The value in parentheses is the representation type. Here a UserId is really just an int wearing a safer name.

extension type Meters(double value) {}

Create One Easily

You construct an extension type like calling a constructor, passing the underlying value straight in.

var id = UserId(42);
var d = Meters(3.5);

Add Safe Methods

Inside the body you can add methods and getters that operate on the wrapped value, giving it real meaning.

extension type Meters(double value) {
  Meters plus(Meters o) => Meters(value + o.value);
}

Stronger Than typedef

A typedef is just an alias, but an extension type is a distinct type. You cannot mix a UserId with a plain int by mistake.

int raw = 42;
// UserId u = raw; // error: not interchangeable

Hide the Underlying API

By default an extension type hides the wrapped type members, so only the methods you expose are callable.

var m = Meters(2.0);
// m.toStringAsFixed(2); // not available unless exposed

Or Implement to Expose

Add implements to forward the underlying members, letting callers use both your API and the base type.

extension type Score(int value) implements int {}
print(Score(9) + 1); // 10

Zero Runtime Overhead

Because no real object is created, extension types cost nothing at runtime. You get type safety for free.

Great for Domain IDs

They shine for typed identifiers like UserId or Email, stopping you from passing the wrong int into the wrong slot.

void load(UserId id) {}
load(UserId(7)); // load(7) would not compile

Extensions vs Extension Types

An extension adds methods to a type everyone shares; an extension type creates a new, distinct type around a value.

Quick Check

Last check: what makes extension types special?

Recap: Extension Types

You met extension types: distinct, zero-cost wrappers over a representation type, ideal for safe IDs and units without runtime overhead. 🏁

Frequently asked questions

Is the “Extension Types for Zero-Cost Wrappers” lesson free?

Yes — the full text of “Extension Types for Zero-Cost Wrappers” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Extension Types for Zero-Cost Wrappers”?

Wrap a type without runtime overhead. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Extension Types for Zero-Cost Wrappers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Writing Your First Extension
  2. Extensions on Generics
  3. Static Extension Members and Conflicts
  4. Extension Types for Zero-Cost Wrappers
← Back to Dart Academy