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

查看:718
本文介绍了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具有两种类型的可选参数: named positional 。在讨论差异之前,让我先讨论相似之处。

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

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

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

在上面的代码中,端口是可选的,其默认值为 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]) {
  // ...
}

可选参数为 positional ,因为如果要指定 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天全站免登陆