-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Rework Gd
constructors, add default()
to engine classes
#486
Conversation
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-486 |
@Bromeon So, if I understand correctly, to use
You mean typying I myself find
I feel like |
Just like let obj = Gd::from_init_fn(|base| {
// accepts the base and returns a constructed object containing it
MyClass { base, other_field: 732 }
});
Thanks for the input! The typing is not really a huge deal, but more that there's not really a way to create engine types without putting them in a I was also thinking about short synonyms like "create", "make", etc. but they are less discoverable and might collide with other Godot methods. Also, how to name
Yep, that's definitely planned, possibly as part of the cheatsheet. |
Exactly - you can't create a IMO it could be looked as a shortcut for
Full agreement
Yeah, that's tricky. I prefer I fully understand that this may seem a little inconsequential as |
About that, it's possible but not that useful, see my previous explanation:
Yeah, I'm getting that feeling too. It's not always obvious whether a type is ref-counted or not, so a user starting to type |
Changes: - Rename `new` -> `from_object` - Rename `new_default` -> `default` - Rename `with_base` -> `from_init_fn` - Extend `impl Default` from user types to engine types that have a new() constructor (i.e. ref-counted ones) - Rename `GodotInit` -> `GodotDefault` to reflect new meaning - Enable `Gd::default()` and `Gd::new_alloc()` constructors depending on memory strategy Old names remain deprecated where possible. Add extension methods `T::new_gd()` and `T::alloc_gd()` as shortcuts for creating Gd<T> instances
There is almost no use case for this, since this is covered by `UserT::alloc_gd()` and `Engine::new_alloc()` in a shorter form. If we need generic programming, we should design it as a whole.
399b2b4
to
8281437
Compare
Changes:
new_default
->default
new
->from_object
with_base
->from_init_fn
impl Default
from user types to engine types that have a new() constructor (i.e. ref-counted ones)GodotInit
->GodotDefault
to reflect new meaningGd::default()
andGd::new_alloc()
constructors depending on memory strategyOld names remain deprecated where possible.
Add extension methods
T::new_gd()
andT::alloc_gd()
as shortcut forGd::<T>::default()
andGd::<T>::new_alloc()
.These are provided via extension trait
UserClass
(part of prelude) and likely almost always the preferred way of constructing.New API overview:
Resource::new()
Node::new_alloc()
Os::singleton()
MyClass::new_gd()
MyClass::alloc_gd()
Gd::<T>::default()
Gd<T>
constructorsGd::<T>::from_object(obj)
base
Gd::<T>::from_init_fn(closure)
Gd::<T>::from_instance_id(id)
Gd::<T>::try_from_instance_id(id)
As you see, the naming is not very consistent, unfortunately. There are some constraints:
MyClass::new()
, since it's very likely that a user already provides such a constructor.Resource::new_gd()
to havenew_gd()
everywhere, but it would mean all engine types are now harder to construct. Unlike with user types, there is no othernew
or so that could collide.Gd::new()
to meanGd::default()
, but:new
not a great choice of name (as this indicates the most commonly used one).from_init_fn()
to be namednew()
just because of how useful it is, and since it's a bit hard to discover otherwise. Lots of people didn't know you could create customGd<T>
instances whereT
would receive a base.This may need a bit more thought, feedback is welcome.