Heads up folks! Only three days ago at Google I/O, the new Dart 3.0 was unveiled! Like a piñata bursting open, it's spilling with a bunch of fun features: records, pattern-matching, and class modifiers. So, grab your biscuits, pour a hot cuppa, and let's dive into this tech fiesta! 🚀
Records
Ever wished your function could return more than one value, like a generous granny handing out sweets? Well, Dart 3.0's got you covered!
Voila! We just made a function return two values. But there's more! Dart 3.0's records have a softer side; they support named parameters. Let's spice up our codebase:
Records quick tip
Neat, isn't it? But what if your record has more fields than a corn farm? Don't worry, Dart has got a neat trick up its sleeve:
Destructuring
Speaking of magic tricks, Dart 3.0 also introduces destructuring. This nifty feature lets you unpack values from arrays or properties from objects into distinct variables, like a magician pulling rabbits out of a hat!
In this magic trick, I created two variables with list destructuring. But the fun doesn't stop there. Did you know you can use destructuring with Dart classes? Brace yourself!
Plus, with destructuring, swapping variable values is as easy as swapping socks:
void main(List<String> args) {
var a = 1;
var b = 2;
[a, b] = [b, a];
print(a); // Prints 2
print(b); // Prints 1
}
Now we're cooking with gas! But wait, there's more. Let's take a look at class modifiers.
Class Modifiers
Now we are moving to classes. Finally, they got modifiers! Let's list them:
interface class - Can't be extended, only implemented. Like a stubborn mule, it refuses to change its ways.
base class: Can't be implemented, only extended. It's like a family heirloom, passing on its legacy.
final class: Can't be extended or implemented. Think of it as the lone wolf of classes.
mixin class: Can be mixed in. It's the social butterfly in the world of classes.
sealed class: Provides exhaustive check. The detective of classes, leaving no stone unturned.
👀
These rules apply only within a library. In the world of Dart, a library equals a file.
It seems we are bending some of these rules, right? That's because these restrictions only apply outside of the library (file). And now, we have the cherry on top - Sealed Classes and Pattern Matching!
Sealed Classes & Pattern Matching
Exhaustive Type Checking is like the ultimate spell check but for your code. It ensures that you've covered all possible scenarios and left no possibility unchecked. Actually, this was already implemented to enums, but Dart 3.0 brings this to sealed classes.
Sealed Classes
And now, let's see this in action with sealed classes:
So, what's the lowdown on this code? We've birthed a variable into existence and we're playing a little game of 'Guess Who'. If our variable 'b' turns out to be the mysterious 'C', we reward ourselves by setting 'isC' to true. If not, alas, 'isC' gets a thumbs down with false. But wait, there's more! Let's crank this up a notch, shall we?
sealed class A {}
class B extends A {}
class C extends A {
C(this.name);
final String name;
}
void main(List<String> args) {
final A b = C("Hello");
// you must include all successors or default case
final isC = switch (b) {
C c => c.name,
_ => "",
};
print(isC); // prints "Hello"
}
I reckon you've already got a taste of how this feature can be as potent as a shot of espresso.
Pattern Matching: Examples
I've cooked up a delightful smorgasbord of pattern matching examples for your coding palate. Feast your eyes on these!
As you can see, the shiny new pattern matching system is like our coding superhero, bestowing upon us the power to create super-efficient exhaustive checks. This kind of approach is like a handy multi-tool, ready to be whipped out for a variety of code conundrums.
Conclusion
Well, we've had a rollicking ride with Dart 3.0! It's been exciting, it's been fun, and there's more coming! So stay tuned for more updates, and remember, in the world of coding, always keep your sense of humor!