Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements with Godot classes for better templating #551

Closed
Esption opened this issue Jan 3, 2024 · 3 comments
Closed

Improvements with Godot classes for better templating #551

Esption opened this issue Jan 3, 2024 · 3 comments
Labels
c: core Core components quality-of-life No new functionality, but improves ergonomics/internals

Comments

@Esption
Copy link

Esption commented Jan 3, 2024

Presently, the only real way to use templates or generic functions with Godot classes involves always calling is with of Inherits<T> and .clone().upcast() either inside the function (with templates) or prior to function call (if not using templates). A basic example with the current system is something like this:

pub fn get_position<T: Inherits<Node2D>>(gd: &Gd<T>) -> Vector2 {
    let gd = gd.clone().upcast();
    gd.get_position()
}

Ideally, the redefine with .clone().upcast() wouldn't be there. The primary two ways I can see this being implemented is

  1. Either make a sort of WeakGD<T> type to at least avoid the clone. The conversion from &'a Gd<T> to WeakGD<'a, U> could just be a .into() call. Since this is actually a hard cast here, an explicit call seems preferable anyway.
  2. Shift functions in the main impl to their own traits. I'm not sure how godot hands off this info in the json as I haven't quite checked, but I think that's probably the "best" solution for making true templates. It should be possible to make use of supertraiting to imitate the inheritance model. This would avoid any sort of cast, at least for templates.

Both are possibly good for different situations, and don't necessarily solve the same thing. Either could make templating marginally smoother though.

As a proof of concept about a WeakGd<T> type see this repo. It definitely could be made better, but it seems to work as intended.

@Bromeon Bromeon added c: core Core components quality-of-life No new functionality, but improves ergonomics/internals labels Jan 3, 2024
@Bromeon
Copy link
Member

Bromeon commented Jan 3, 2024

Yep, I agree this should become more ergonomic 🙂

I don't think it warrants a new WeakGd type though.
Wouldn't an upcast_ref()/upcast_mut() function pair achieve the same?

pub fn get_position<T: Inherits<Node2D>>(gd: &Gd<T>) -> Vector2 {
    let node: &Node2D = gd.upcast_ref();
    node.get_position()
}

There's some details to be worked out, e.g. whether we want this on Gd<T> or T, but you get the idea.

@vortexofdoom
Copy link
Contributor

@Bromeon
Copy link
Member

Bromeon commented Feb 3, 2024

@vortexofdoom yes, but it didn't at the time I wrote that comment. They were added a few days later in #558 🙂

But you're right, I think this can be closed.

@Bromeon Bromeon closed this as completed Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Core components quality-of-life No new functionality, but improves ergonomics/internals
Projects
None yet
Development

No branches or pull requests

3 participants