What's the closest thing rust has to ergonomic type specializations of generic functions via point free definition?

Here's what I want:

pub fn despawn_where<F: WorldQuery>(
    to_despawn: Query<Entity, F>,
    mut commands: Commands,
) {
    to_despawn.for_each(|e| {
        commands.entity(e).despawn();
    });
}

// pub fn despawn_level_contents(
//     to_despawn: Query<Entity, Or<(With<Guy>, With<Wall>, With<Portal>)>>,
//     commands: Commands,
// ) {
//     despawn_where(to_despawn, commands)
// }

pub fn despawn_level_contents = despawn_where::<Or<(With<Guy>, With<Wall>, With<Portal>)>>>;

Long shot, but does rust support anything at all like this?

Can you give us some more context here?

From your post, it's not really clear what you are asking for and whether all the types/traits your snippet mentions are hypothetical things you'd like to implement or taken from some other crate.

it doesn't, however, you can use generic system with bevy, which should be what you're looking for: Generic Systems - Unofficial Bevy Cheat Book

Sorry, to clarify, the types exist and are from bevy. What I'm asking about is whether rust has some ergonomic way of writing type specialized versions of generic functions. Here, the commented out version of despawn_level_contents is such a specialization of despawn_where. The definition is trivial but a little boilerplatey, I was wondering whether there was a nicer way.

Thanks. Yeah, tbc I'm aware I can use generics with systems, I am using despawn_where and the commented out version of despawn_level_contents, which works just fine. I was just hoping there was some more concise way to write despawn_level_contents. I wrote despawn_level_contents because I didn't want to have to write

app
  // ...
  .add_enter_system(AppState::Loading, despawn_where::<Or<(With<Guy>, With<Wall>, With<Portal>)>>>)

I can instead have

app
  // ...
  .add_enter_system(AppState::Loading, despawn_level_contents)

Well, that seems like a minimal convenience. Actually it seems you'd have to write more code by doing it your way, BUT, you could store the specialized fn in a variable and then use that

let despawn_level_contents = despawn_where::<Or<(With<Guy>, With<Wall>, With<Portal>)>>>;
app.add_enter_system(AppState::Loading, despawn_level_contents)

You could also store it in a static global variable, but it'd be more boilerplaty

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.