Dart 3.3 news: what are Extension Types?
Tags: flutter, dart, prograaming, code
Published: 2024-02-16 01:38:38

- They’re a way to enhance existing types with specialized methods and properties without incurring the memory overhead of typical wrapper classes.
- Think of them as lightweight “extensions” that add functionality like custom getters, setters, operators, and functions specifically tailored to the underlying type.

**Key Advantages:**

**Enhanced Performance: **They avoid creating dedicated objects (wrappers) for each instance, making them ideal for performance-sensitive scenarios, especially when dealing with large datasets or frequent data manipulation.

**Clearer Abstraction:** You can create clean Dart-like APIs to hide the complexities of underlying representation types, promoting better code readability and maintainability.

**Simplified Interoperability:** They streamline working with native platform types (host platforms). You can interact with them directly, eliminating the need for separate wrappers and reducing potential performance bottlenecks.
syntax and Usage

```
extension type NumberExtension(int i) {
  // Custom methods and properties specific to integers
  bool isEven() => i % 2 == 0;
  String doubleIt() => 'Double: ${i * 2}';
}

void main() {
  final number = NumberExtension(13); // Use extension type notation

    print('Double it : ${number.doubleIt()}');
    print('Is Even: ${number.isEven()}');
  
}
```
**Key Points:**

- **They’re zero-cost: **Dart generates code as if you were working with the underlying type directly.
- They define a new type, distinct from the underlying type, but provide type safety.

- They can’t change the behavior of the existing type’s properties and methods.

**

## Differences Between Extension Types and Extension Methods:
**

- Extension Methods:

Allow adding methods and properties to existing types without creating a new type.
Are lightweight and suitable for adding simple functionalities.
Cannot be used to define a new type with entirely different behavior.
**Extension Types:**

Allow creating a new type that encapsulates the original type and provides a customized API.
Are more powerful and can be used to add complex functionalities.
Are ideal for interoperability with native platforms and performance optimization.
that is:

- Extension Methods are ideal for adding simple functionalities to existing types.
- Extension Types are ideal for creating custom APIs and optimizing performance.
example:

**Example:**

Suppose we want to add an isEven() method to check if a number is even.

With Extension Methods:

```

Dart
extension IsEven on int {
  bool isEven() => this % 2 == 0;
}

void main() {
  final number = 42;
  print(number.isEven()); // true
}
```
With Extension Types:

```
extension type EvenNumber(int i) {
  bool isEven() => i % 2 == 0;
}

void main() {
  final number = EvenNumber(42);
  print(number.isEven()); // true
}
```
In this example, both solutions work, but the latter is more robust and flexible. By using an Extension Type, we can create a new type EvenNumber with specific behavior. This can be useful for interoperability with native platforms or for performance optimization.

Other differences:

- Extension Types can have constructors, while Extension Methods cannot.

**In general:**

- Use Extension Methods for simple functionalities.
- Use Extension Types for complex functionalities, interoperability with native platforms, or performance optimization.
- In Conclusion:
- Extension Types are a powerful tool for performance optimization, code clarity, and seamless interoperability in Dart 3.3. They empower you to create highly efficient and well-structured applications, especially when dealing with native code or performance-critical domains