CopyPastor

Detecting plagiarism made easy.

Score: 2; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2022-09-18
by Tasnuva Tavasum oshin

Original Post

Original - Posted on 2012-11-07
by Seth Ladd



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Dart has two types of optional parameters: _named_ and _positional_. Before I discuss the differences, let me first discuss the similarities.
Dart's optional parameters are _optional_ in that the caller isn't required to specify a value for the parameter when calling the function.
Optional parameters can only be declared after any required parameters.
Optional parameters can have a default value, which is used when a caller does not specify a value.
**Positional optional parameters**
A parameter wrapped by `[ ]` is a positional optional parameter. Here is an example:
<!-- language-all: lang-dart -->
getHttpUrl(String server, String path, [int port=80]) { // ... }
In the above code, `port` is optional and has a default value of `80`.
You can call `getHttpUrl` with or without the third parameter.
getHttpUrl('example.com', '/index.html', 8080); // port == 8080 getHttpUrl('example.com', '/index.html'); // port == 80
You can specify multiple positional parameters for a function:
getHttpUrl(String server, String path, [int port=80, int numRetries=3]) { // ... }
The optional parameters are _positional_ in that you can't omit `port` if you want to specify `numRetries`.
getHttpUrl('example.com', '/index.html'); getHttpUrl('example.com', '/index.html', 8080); getHttpUrl('example.com', '/index.html', 8080, 5);
Of course, unless you know what 8080 and 5 are, it's hard to tell what those apparently magic numbers are. You can use _named optional parameters_ to create more readable APIs.
**Named optional parameters**
A parameter wrapped by `{ }` is a named optional parameter. Here is an example:
getHttpUrl(String server, String path, {int port = 80}) { // ... }
You can call `getHttpUrl` with or without the third parameter. You **must** use the parameter name when calling the function.
getHttpUrl('example.com', '/index.html', port: 8080); // port == 8080 getHttpUrl('example.com', '/index.html'); // port == 80
You can specify multiple named parameters for a function:
getHttpUrl(String server, String path, {int port = 80, int numRetries = 3}) { // ... }
Because named parameters are referenced by name, they can be used in an order different from their declaration.
getHttpUrl('example.com', '/index.html'); getHttpUrl('example.com', '/index.html', port: 8080); getHttpUrl('example.com', '/index.html', port: 8080, numRetries: 5); getHttpUrl('example.com', '/index.html', numRetries: 5, port: 8080); getHttpUrl('example.com', '/index.html', numRetries: 5);
I believe named parameters make for easier-to-understand call sites, especially when there are boolean flags or out-of-context numbers.
**Checking if optional parameter was provided**
Unfortunately, you cannot distinguish between the cases "an optional parameter was not provided" and "an optional parameter was provided with the default value".
**Note:** You may use positional optional parameters **or** named optional parameters, _but not both_ in the same function or method. The following is not allowed.
thisFunctionWontWork(String foo, [String positonal], {String named}) { // will not work! }
Dart has two types of optional parameters: _named_ and _positional_. Before I discuss the differences, let me first discuss the similarities.
Dart's optional parameters are _optional_ in that the caller isn't required to specify a value for the parameter when calling the function.
Optional parameters can only be declared after any required parameters.
Optional parameters can have a default value, which is used when a caller does not specify a value.
**Positional optional parameters**
A parameter wrapped by `[ ]` is a positional optional parameter. Here is an example:
<!-- language-all: lang-dart -->
getHttpUrl(String server, String path, [int port=80]) { // ... }
In the above code, `port` is optional and has a default value of `80`.
You can call `getHttpUrl` with or without the third parameter.
getHttpUrl('example.com', '/index.html', 8080); // port == 8080 getHttpUrl('example.com', '/index.html'); // port == 80
You can specify multiple positional parameters for a function:
getHttpUrl(String server, String path, [int port=80, int numRetries=3]) { // ... }
The optional parameters are _positional_ in that you can't omit `port` if you want to specify `numRetries`.
getHttpUrl('example.com', '/index.html'); getHttpUrl('example.com', '/index.html', 8080); getHttpUrl('example.com', '/index.html', 8080, 5);
Of course, unless you know what 8080 and 5 are, it's hard to tell what those apparently magic numbers are. You can use _named optional parameters_ to create more readable APIs.
**Named optional parameters**
A parameter wrapped by `{ }` is a named optional parameter. Here is an example:
getHttpUrl(String server, String path, {int port = 80}) { // ... }
You can call `getHttpUrl` with or without the third parameter. You **must** use the parameter name when calling the function.
getHttpUrl('example.com', '/index.html', port: 8080); // port == 8080 getHttpUrl('example.com', '/index.html'); // port == 80
You can specify multiple named parameters for a function:
getHttpUrl(String server, String path, {int port = 80, int numRetries = 3}) { // ... }
Because named parameters are referenced by name, they can be used in an order different from their declaration.
getHttpUrl('example.com', '/index.html'); getHttpUrl('example.com', '/index.html', port: 8080); getHttpUrl('example.com', '/index.html', port: 8080, numRetries: 5); getHttpUrl('example.com', '/index.html', numRetries: 5, port: 8080); getHttpUrl('example.com', '/index.html', numRetries: 5);
I believe named parameters make for easier-to-understand call sites, especially when there are boolean flags or out-of-context numbers.
**Checking if optional parameter was provided**
Unfortunately, you cannot distinguish between the cases "an optional parameter was not provided" and "an optional parameter was provided with the default value".
**Note:** You may use positional optional parameters **or** named optional parameters, _but not both_ in the same function or method. The following is not allowed.
thisFunctionWontWork(String foo, [String positonal], {String named}) { // will not work! }

        
Present in both answers; Present only in the new answer; Present only in the old answer;