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

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

问题描述

Dart 支持命名可选参数和位置可选参数.两者有什么区别?

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 有两种类型的可选参数:namedpositional.在讨论不同点之前,让我先讨论一下相同点.

Dart has two types of optional parameters: named and positional. Before I discuss the differences, let me first discuss the similarities.

Dart 的可选参数是可选,因为调用者在调用函数时不需要为参数指定值.

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.

位置可选参数

[ ] 包裹的参数是一个位置可选参数.下面是一个例子:

A parameter wrapped by [ ] is a positional optional parameter. Here is an example:

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

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

In the above code, port is optional and has a default value of 80.

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

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]) {
  // ...
}

可选参数是位置,因为如果要指定numRetries,则不能省略port.

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

当然,除非您知道 8080 和 5 是什么,否则很难说出那些看似神奇的数字是什么.您可以使用命名的可选参数来创建更具可读性的 API.

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.

命名的可选参数

{ } 包裹的参数是一个命名的可选参数.下面是一个例子:

A parameter wrapped by { } is a named optional parameter. Here is an example:

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

您可以使用或不使用第三个参数调用 getHttpUrl.您在调用函数时必须使用参数名称.

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.

检查是否提供了可选参数

很遗憾,您无法区分未提供可选参数"和已为可选参数提供默认值"两种情况.

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

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