Optional Arguments

One of the more tedious chores in most languages is extracting optional arguments. Dart’s take on the matter is an altogether pleasant built-in syntax.

Let’s say that we want a function named good_day to print a pleasant greeting, with an optional flag to indicate a follow-up message. Calling such a function in Dart looks like the following.

functional_programming/optional.dart
 
good_day(​"Bob"​);
 
// Good day, Bob.
 
good_day(​"Bob"​, emphatic: true);
 
// Good day, Bob.
 
// I said good day!

In the second call, we supply the optional emphatic: true option to get the extra message. At first glance, this might look like a HashMap. Closer inspection reveals that there are no curly braces around it. This is not a HashMap, though the similarity is intentional. Rather this is Dart’s built-in syntax to supply an optional parameter.

To declare a parameter as optional in a function definition, wrap it inside curly braces. The good_day function, with a required name parameter and an optional emphatic parameter, can be defined as:

 
good_day(name, {emphatic}) {
 
print(​"Good day, ${name}."​);
 
if​ (emphatic) {
 
print(​"I said good day!"​);
 
}
 
}

This is a wonderful syntax. At the same time, it is easy to understand what is intended and there is very little clutter. Even if you are new to Dart, the curly braces evoke a sense of named parameters by virtue of their resemblance to JSON object literals. With this, the body of the function can focus on its inherent purpose—no options parsing to obscure intent.

Named optional parameters support more than one value by separating them with commas. It is even possible to supply default values by including a colon after the parameter name.

Consider the following profile, which prints out a name and, optionally, a personal hero and favorite color. Since Weird Al Yankovic is the personal hero to a majority of hipsters, we can supply him as the default value as follows:

 
profile(name, {hero:​"Weird Al"​, favorite_color}) {
 
print(​"Name: ${name}"​);
 
print(​" personal hero: ${hero}"​);
 
if​ (favorite_color != null) {
 
print(​" favorite color: ${favorite_color}"​);
 
}
 
}

With that, if we profile Bob, we find:

 
profile(​"Bob"​);
 
// Name: Bob
 
// personal hero: Weird Al

Should Bob prove fickle and change his personal hero and decide upon a favorite color, a subsequent call to profile might result in:

 
profile(​"Bob"​, favorite_color: ​'Purple'​, hero: ​'Frank Drebin'​);
 
// Name: Bob
 
// personal hero: Frank Drebin
 
// favorite color: Purple

Named, optional parameters are an exceptional aid for writing clean code, but Dart does not stop there. Dart also supports positional parameters. That is, if we want the first parameter to a function to be required but the next two to be optional, then Dart has us covered.

Since named, optional parameters adopt a HashMap feel, it should not be a surprise that positional, optional parameters adopt a List feel. So, instead of curly braces, we use square brackets in the function definition:

 
movie(title, [starring=​"Leslie Nielson"​, co_starring]) {
 
print(​"Great movie: ${title}"​);
 
print(​" Starring: ${starring}"​);
 
if​ (co_starring != null) {
 
print(​" Co-starring: ${co_starring}"​);
 
}
 
}

As you can see, positional parameters also support default values—though an equals sign is used instead of a colon. Calling this function is a simple matter of invoking it with just the required value or one or more of the optional values.

 
movie(​"The Naked Gun"​);
 
// Great movie: The Naked Gun
 
// Starring: Leslie Nielson
 
movie(​"Airplane!"​, ​"Robert Hays"​, ​"Leslie Nielson"​);
 
// Great movie: The Naked Gun
 
// Starring: Robert Hays
 
// Co-starring: Leslie Nielson

This certainly beats rooting through object literals. Optional arguments are even more powerful inside class and instance methods, as you’ll see in Chapter 7, Classes and Objects.

Recipe 7 Note: this make sense in Dart. One of the ways in which Dart departs from JavaScript is in the use of the this keyword. Dart’s stance on the matter is that this has absolutely nothing to do with the current function. Instead, this is reserved for objects and always refers to the current object. There is no binding of this, applying this, or calling this in Dart. In other words, this has nothing to do with functions. We’ll discuss this again in Chapter 7, Classes and Objects, but only briefly because this is so dang simple in Dart!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.70.21