Dart中的named和optional参数有什么区别? [英] What is the difference between named and optional parameters in Dart?

查看:228
本文介绍了Dart中的named和optional参数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dart支持命名的可选参数和位置可选参数。



另外,如何判断是否实际指定了可选参数?

命名为和位置



Dart的可选参数是可选,因为调用者不需要指定



可选参数只能在任何必需参数后声明。



可选参数可以有默认值,当调用者未指定值时使用。



位置可选参数

[] 包装的参数是位置可选参数。下面是一个示例:

  getHttpUrl(String server,String path,[int port = 80]){
// ...
}

在上面的代码中, port 是可选的,默认值为 80



您可以使用或不使用第三个参数调用 getHttpUrl

  getHttpUrl('example.com','/index.html',8080); // port == 8080 
getHttpUrl('example.com','/index.html'); // port == 80

您可以为函数指定多个位置参数:

  getHttpUrl(String server,String path,[int port = 80,int numRetries = 3]){
// ...
}

可选参数是 em>,如果您要指定 numRetries ,则不能省略 port

  getHttpUrl('example.com','/index.html'); 
getHttpUrl('example.com','/index.html',8080);
getHttpUrl('example.com','/index.html',8080,5);

当然,除非你知道8080和5是什么,是。您可以使用命名的可选参数创建更多可读的API。



命名的可选参数
$ b

{} 包装的参数是一个命名的可选参数。这里是一个例子:

  getHttpUrl(String server,String path,{int port:80} ){
// ...
}

c $ c> getHttpUrl 有或没有第三个参数。您必须在调用该函数时使用参数名称。

  getHttpUrl ('example.com','/index.html',port:8080); // port == 8080 
getHttpUrl('example.com','/index.html'); // port == 80

您可以为函数指定多个命名参数:

  getHttpUrl(String server,String path,{int port:80,int numRetries:3}){
// ...
}

因为命名参数是按名称引用的,

  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);

我相信命名参数使得更容易理解的调用站点,特别是当有布尔标志或


$ b

检查是否提供了可选参数

您可以检测是否提供了一个可选参数,如果提供了该参数则返回true(即使设置为 null !)

  beAwesome([int scale]){
if (!?scale){
print('scale not specified');
} else if(scale == null){
print('no awesome');
} else {
print('Awesome times $ scale');
}
}

beAwesome(10000); // Awesome times 10000
beAwesome(null); // no awesome
beAwesome(); // scale not specified

注意:您可以使用位置可选参数命名的可选参数,但不能同时包含。以下是不允许的。

  thisFunctionWontWork(String foo,[String positonal] }){
//将无法工作!
}


Dart supports both named optional parameters and positional optional parameters. What are the differences between the two?

Also, how can you tell if an optional parameter was actually specified?

解决方案

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:

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

You can detect if an optional parameter was provided with ?, which returns true if the parameter was provided (even if it was set to null!)

beAwesome([int scale]) {
  if (!?scale) {
    print('scale not specified');
  } else if (scale == null) {
    print('no awesome');
  } else {
    print('Awesome times $scale');
  }
}

beAwesome(10000);  // Awesome times 10000
beAwesome(null);   // no awesome
beAwesome();       // scale not specified

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中的named和optional参数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆