Skip to content

Commit 54e548c

Browse files
committed
Update docs for fallback branch
1 parent 81dafeb commit 54e548c

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

godot-core/src/classes/match_class.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
/// The current implementation checks [`Gd::try_cast()`][crate::obj::Gd::try_cast] linearly with the number of branches.
1212
/// This may change in the future.
1313
///
14-
/// Only requires a fallback branch if the type of the expression is different than `()`, and in such case it's required even if all direct known classes are handled. The reason for this is that there may be other subclasses which
15-
/// are not statically known by godot-rust (e.g. from a script or GDExtension), and the arm needs to return a value of that type. The fallback branch can either be `_` (discard object), or
16-
/// `variable` to access the original object inside the fallback arm.
14+
/// When none of the listed classes match, a _fallback branch_ acts as a catch-all and allows to retrieve the original `Gd` pointer.
15+
/// If the type of the `match_class!` expression is `()`, you can omit the fallback branch. For all other types, it is required, even if all
16+
/// direct subclasses are handled. The reason for this is that there may be other subclasses which are not statically known by godot-rust
17+
/// (e.g. from a script or GDExtension).
18+
///
19+
/// The fallback branch can either be `_` (discard object), or `variable` to access the original object inside the fallback arm.
1720
///
1821
/// # Example
1922
/// ```no_run
@@ -51,8 +54,7 @@
5154
/// original => 0,
5255
/// // Can also be used with mut:
5356
/// // mut original => 0,
54-
/// // It can't be left out because the expression has a type.
55-
/// // Otherwise, we could've skipped this branch.
57+
/// // If the match arms have type (), we can also omit the fallback branch.
5658
/// };
5759
///
5860
/// // event_type is now 0, 1, 2, or 3
@@ -109,7 +111,8 @@ macro_rules! match_class_muncher {
109111
$block
110112
}};
111113

112-
// _ => { ... } or nothing, if there is no need for a return value.
114+
// _ => { ... }
115+
// or nothing, if fallback is absent and overall expression being ().
113116
($subject:ident, $(_ => $block:expr $(,)?)?) => {{
114117
$($block)?
115118
}};

itest/rust/src/engine_tests/match_class_test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ fn match_class_named_fallback_unmatched() {
136136
Resource::new_gd().upcast::<Object>(),
137137
_node @ Node => 1,
138138
_res @ Resource => 2,
139-
_ignored => 3,
140139
};
141140

142141
assert_eq!(result, 2);

0 commit comments

Comments
 (0)