英文:
Dart nullable records with pattern matching: The matched value of type '(String, int)?' isn't assignable to the required type '(Object?, Object?)'
问题
(final String? word, final int? number) = getPair();
英文:
Given the following function:
(String, int)? getPair() {
return ('hello', 42);
}
I can access the multiple returns using the fields like so:
final pair = getPair();
if (pair != null) {
String word = pair.$1;
int number = pair.$2;
}
However, I'd prefer to destructure the values using some sort of pattern matching. This is what I tried:
final (word, number) = getPair();
But that gives the following error:
> The matched value of type '(String, int)?' isn't assignable to the required type '(Object?, Object?)'.
> Try changing the required type of the pattern, or the matched value type.
How would I change the required type of the pattern or the matched value?
Related reference:
答案1
得分: 2
你可以对结果进行模式匹配并提取:
void main(List<String> arguments) {
final (word, number) = switch (getPair()) {
(String s, int i) => (s, i),
final v => throw Exception('$v did not match'),
};
print('word is $word, number is $number');
}
(String, int)? getPair() {
return ('hello', 42);
}
如果你需要变量在有限的范围内使用,你可以使用 if-case:
void main(List<String> arguments) {
if (getPair() case (final String word, final int number)) {
print('word is $word, number is $number');
}
}
(String, int)? getPair() {
return ('hello', 42);
}
英文:
You can pattern match on the result and extract with that:
void main(List<String> arguments) {
final (word, number) = switch (getPair()) {
(String s, int i) => (s, i),
final v => throw Exception('$v did not match'),
};
print('word is $word, number is $number');
}
(String, int)? getPair() {
return ('hello', 42);
}
If you need the vars for a limited scope, you can use if-case:
void main(List<String> arguments) {
if (getPair() case (final String word, final int number)) {
print('word is $word, number is $number');
}
}
(String, int)? getPair() {
return ('hello', 42);
}
答案2
得分: 1
在`null`检查之后,你可以进行解构,就像你手动使用`$1`和`$2`一样:
```dart
final pair = getPair();
if (pair != null) {
final (word, number) = pair;
// ...
}
你可以将非null
断言 作为声明模式的一部分:
final ((word, number)!) = getPair();
如果值为null
,将会抛出异常。
你不能在声明中进行null
检查,因为声明模式必须是不可反驳的,而检查可能失败。
你可以在可反驳模式检查的一部分中进行空检查,使用?
:
if (getPair() case (final word, final number)?) {
// ...
} // else { "or else, if needed"; }
或者
switch (getPair()) {
case (final word, final number)?: // ....
// default: "or else, if needed";
}
由于在后续解构中还匹配了类型,可以删除?
。
或者,如果你还想要绑定pair
,你可以结合这两种方法:
if (getPair() case final pair?) { // 如果不为`null`,则绑定`pair`。
final (word, number) = pair;
}
英文:
You can do that destructuring after the null
-check, like you did manually using $1
and $2
:
final pair = getPair();
if (pair != null) {
final (word, number) = pair;
// ...
}
You can do a not-null
assert as part of a declaration pattern:
final ((word, number)!) = getPair();
That will throw if the value is null
.
You cannot do a null
check as part of a declaration, because declaration patterns must be irrefutable, and a check can fail.
You can do the null check as part of a refutable pattern check, using ?
:
if (getPair() case (final word, final number)?) {
// ...
} // else { "or else, if needed"; }
or
switch (getPair()) {
case (final word, final number)?: // ....
// default: "or else, if needed";
}
Since you're then also matching the type in the following destructuring, the ?
can be removed.
Or you can combine the two approaches, if you also want the binding of pair
:
if (getPair() case final pair?) { // Bind `pair` if it's not `null`.
final (word, number) = pair;
}
答案3
得分: 0
这是一个比我想要的更冗长一些的解决方案,但这是一个部分解决方案:
final pair = getPair();
if (pair == null) return; // or throw/break/continue
final (word, number) = pair;
如果有更直接的方法,我欢迎另一个答案。
英文:
Well, this is a little more verbose that what I was looking for, but this is a partial solution:
final pair = getPair();
if (pair == null) return; // or throw/break/continue
final (word, number) = pair;
I welcome another answer if there is a more direct way to do it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论