-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[Merged by Bors] - default() shorthand #4071
Conversation
Some nits, but I like this change. It's about equally clear, shorter, and gives us a nice place to stick helpful docs. IMO we should strongly consider applying this in |
Co-authored-by: Alice Cecile <[email protected]>
I'm assuming you mean "uses of bevy_ui", not bevy_ui itself (which only has 3 uses of Default::default()? |
Yes, in the usages. Perhaps re-export it in the |
longer term, I would like to depend less on the |
I'm open to it, but my gut reaction is that three export locations is already more than enough (
Yeah as soon as a better alternative presents itself I'm open to making a switch. But |
bors try |
I'll offer my dissenting view. I don't like I would personally prefer something simple like use bevy::prelude::*;
struct Foo {
bar: usize,
baz: usize,
}
impl Foo {
const fn default() -> Self { Self { bar: 0, baz: 0 } }
}
let myfoo = Foo {
bar: 10,
..Foo::default()
}; (not necessarily advocating for using 'default' as the name. The name of fn could be anything pros
con
|
I dont really see the difference here. ".." is a special language feature. We are already using Rust as it is defined. Every option considered involves calling a function. The only difference is that this function is floating (and generic).
This is nice, but I consider this secondary to the goal of providing easy, ergonomic struct declarations.
I'm perfectly content to let users discover this on their own time. I consider "teaching rust" a secondary goal to "using bevy".
It does do that, but at the cost of significant declaration boilerplate, due to the inability of the const method to be derived, and the "stutter" of specifying the type name twice. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish rust offered cleaner options for this, but as it stands today this is the best we can do.
just to clarify my positions on some things
Sorry for not being clear. I meant that
I was trying to get across that personally I don't think
Its not solving stutter 'at the cost of' boilerplate. Not all boilerplate is bad, particularly if it aids in reasoning, comprehension, and good errors - I'd refer to Also note the (cognitive) ambiguity between examplecommands.spawn_bundle(TextBundle {
style: Style {
margin: Rect::all(Val::Px(5.0)),
..Default::default()
},
text: Text::with_section(
"Text Example",
TextStyle {
font_size: 30.0,
font: Default::default(),
color: Color::RED,
},
Default::default(),
),
..Default::default()
}); anyway, I don't think I changed any hearts today |
One last comment. There is actually another option that doesn't require a function call (but does require some namespacing) and is in some ways better, but in others worse: #[derive(Debug)]
struct Foo {
bar: usize,
baz: usize,
}
impl Foo {
const DEFAULT: Self = Self { bar: 0, baz: 0 };
}
let foo = Foo {
bar: 10,
..Foo::DEFAULT
}; |
@ickk ok I do understand your perspective now. You make some good points, but I still think |
bors r+ |
Adds a `default()` shorthand for `Default::default()` ... because life is too short to constantly type `Default::default()`. ```rust use bevy::prelude::*; #[derive(Default)] struct Foo { bar: usize, baz: usize, } // Normally you would do this: let foo = Foo { bar: 10, ..Default::default() }; // But now you can do this: let foo = Foo { bar: 10, ..default() }; ``` The examples have been adapted to use `..default()`. I've left internal crates as-is for now because they don't pull in the bevy prelude, and the ergonomics of each case should be considered individually.
Necropost I've made this known elsewhere, but I'm of the strong opinion that reducing character count for its own sake is antithetical to ergonomics, especially if it breaks from ecosystem conventions.
In isolation this PR isn't really a big deal, but I'd like to voice the opinion that this change is counter to its goals. |
Adds a `default()` shorthand for `Default::default()` ... because life is too short to constantly type `Default::default()`. ```rust use bevy::prelude::*; #[derive(Default)] struct Foo { bar: usize, baz: usize, } // Normally you would do this: let foo = Foo { bar: 10, ..Default::default() }; // But now you can do this: let foo = Foo { bar: 10, ..default() }; ``` The examples have been adapted to use `..default()`. I've left internal crates as-is for now because they don't pull in the bevy prelude, and the ergonomics of each case should be considered individually.
Adds a
default()
shorthand forDefault::default()
... because life is too short to constantly typeDefault::default()
.The examples have been adapted to use
..default()
. I've left internal crates as-is for now because they don't pull in the bevy prelude, and the ergonomics of each case should be considered individually.