<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ildeberto Silva</title>
    <description>The latest articles on DEV Community by Ildeberto Silva (@ildysilva).</description>
    <link>https://dev.to/ildysilva</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1110115%2F87319f29-0fde-4a0c-9d84-278f25b45988.jpeg</url>
      <title>DEV Community: Ildeberto Silva</title>
      <link>https://dev.to/ildysilva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ildysilva"/>
    <language>en</language>
    <item>
      <title>Dart 3.3 news: what are Extension Types?</title>
      <dc:creator>Ildeberto Silva</dc:creator>
      <pubDate>Fri, 16 Feb 2024 01:38:38 +0000</pubDate>
      <link>https://dev.to/ildysilva/dart-33-news-what-are-extension-types-1501</link>
      <guid>https://dev.to/ildysilva/dart-33-news-what-are-extension-types-1501</guid>
      <description>&lt;ul&gt;
&lt;li&gt;They’re a way to enhance existing types with specialized methods and properties without incurring the memory overhead of typical wrapper classes.&lt;/li&gt;
&lt;li&gt;Think of them as lightweight “extensions” that add functionality like custom getters, setters, operators, and functions specifically tailored to the underlying type.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;**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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clearer Abstraction:&lt;/strong&gt; You can create clean Dart-like APIs to hide the complexities of underlying representation types, promoting better code readability and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simplified Interoperability:&lt;/strong&gt; 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.&lt;br&gt;
syntax and Usage&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension type NumberExtension(int i) {
  // Custom methods and properties specific to integers
  bool isEven() =&amp;gt; i % 2 == 0;
  String doubleIt() =&amp;gt; 'Double: ${i * 2}';
}

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

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

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;**They’re zero-cost: **Dart generates code as if you were working with the underlying type directly.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;They define a new type, distinct from the underlying type, but provide type safety.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;They can’t change the behavior of the existing type’s properties and methods.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Differences Between Extension Types and Extension Methods:
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extension Methods:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Allow adding methods and properties to existing types without creating a new type.&lt;br&gt;
Are lightweight and suitable for adding simple functionalities.&lt;br&gt;
Cannot be used to define a new type with entirely different behavior.&lt;br&gt;
&lt;strong&gt;Extension Types:&lt;/strong&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Extension Methods are ideal for adding simple functionalities to existing types.&lt;/li&gt;
&lt;li&gt;Extension Types are ideal for creating custom APIs and optimizing performance.
example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose we want to add an isEven() method to check if a number is even.&lt;/p&gt;

&lt;p&gt;With Extension Methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Dart
extension IsEven on int {
  bool isEven() =&amp;gt; this % 2 == 0;
}

void main() {
  final number = 42;
  print(number.isEven()); // true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Extension Types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension type EvenNumber(int i) {
  bool isEven() =&amp;gt; i % 2 == 0;
}

void main() {
  final number = EvenNumber(42);
  print(number.isEven()); // true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Other differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extension Types can have constructors, while Extension Methods cannot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In general:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Extension Methods for simple functionalities.&lt;/li&gt;
&lt;li&gt;Use Extension Types for complex functionalities, interoperability with native platforms, or performance optimization.&lt;/li&gt;
&lt;li&gt;In Conclusion:&lt;/li&gt;
&lt;li&gt;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&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>prograaming</category>
      <category>code</category>
    </item>
  </channel>
</rss>
