Improve clarity of condition.rs doc examples#24916
Conversation
|
Maybe use a |
I tried Also, the specific /// # use bevy_ecs::prelude::*;
/// # #[derive(Resource, Default, PartialEq)]
/// # struct Counter(u8);
/// # let mut schedules = Schedules::default();
/// # let mut world = World::new();
/// schedules.add_systems(
/// Update,
/// // `resource_exists_and_equals` will only return true
/// // if the given resource exists and equals the given value
/// my_system.run_if(resource_exists_and_equals(Counter(0))),
/// );
///
/// fn my_system(mut counter: ResMut<Counter>) {
/// counter.0 += 1;
/// }
///
/// let schedule = schedules.get_mut(Update).unwrap();
///
/// // `Counter` hasn't been added so `my_system` can't run
/// schedule.run(&mut world);
/// world.init_resource::<Counter>();
///
/// // `Counter` is `0` so `my_system` can run
/// schedule.run(&mut world);
/// assert_eq!(world.resource::<Counter>().0, 1);
///
/// // `Counter` is no longer `0` so `my_system` won't run
/// schedule.run(&mut world);
/// assert_eq!(world.resource::<Counter>().0, 1);
I opted to keep the /// # let app = app.get_mut(Update).unwrap();
It seems dishonest to me, but maybe it makes the example clearer to view. Not sure if that's acceptable.🤷♂️ |
|
So I believe the questions I have are:
|
This is a nice pattern, you can use # to hide the distracting setup lines.
Yeah :) |
|
Now the examples look indistinguishable from normal app code with the exception of many instances of Exampleapp.add_systems(
Update,
my_system.run_if(on_message::<MyMessage>),
);
#[derive(Message)]
struct MyMessage;
fn my_system(mut counter: ResMut<Counter>) {
counter.0 += 1;
}
// No new `MyMessage` messages have been pushed so `my_system` won't run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);
world.resource_mut::<Messages<MyMessage>>().write(MyMessage);
// A `MyMessage` message has been pushed so `my_system` will run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);I personally think it would be least confusing to just keep the type as |
Yeah, now that I can see the actual diff I think you're right 😅 Sorry for making you run around! |
|
All good :) Should be ready for review. |
My issue with this is that people will copy the code and it won't work for them. If they understand what's going on they'll just use the It's at worst just a minor issue, so I'm fine with the pr as is. |
|
Yeah, I think best case scenario is something like depending on bevy_app only for doctests (not sure if that’s possible though). But in my eyes, it’s more important for the doc to be truthful than for it to be easily copy-pasteable. Part of the greatness of doctests is that it is easy to detect when they stop telling the truth about API surface because they will stop compiling. Mocking App means that it won’t necessarily have that ability anymore. When I look at these docs as a user, I can see myself trying to use add_systems without the Schedule param, failing, then thinking to myself “hmm, it must be wrong because it’s called schedule, I need to investigate further”. But if it’s named app and it fails (either because we couldn’t fully mock it due to technical reasons or because the real App API changed and the mock wasn’t updated to match due to the mock still compiling), then I can see myself as a user not only being more confused (it’s called app it should work), but I can also see myself losing trust in the doc examples. “I have to look closer into the examples for the truth because sometimes it’s not how it appears”. But I agree, it’s a pretty small issue either way. |
Objective
A variable named
appcallingadd_systemis potentially confusing since the variable is actually of typeSchedule, and thus doesn't specify a schedule in the call.Fixes #11814
Alternative to #11815
Solution
Rename app to schedule to match the actual type being used.
Alternatives
ScheduleLabelfor each noncompile_faildoc example, and mockapp.add_systemswith it.