Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

Why the new methods? #78

Open
bowbahdoe opened this issue Jun 3, 2019 · 1 comment
Open

Why the new methods? #78

bowbahdoe opened this issue Jun 3, 2019 · 1 comment

Comments

@bowbahdoe
Copy link

bowbahdoe commented Jun 3, 2019

(Sorry if this is out of place or too simple a question.)

Why do these components have static new methods if all of their fields are public? Aren't they construct-able outside the module by listing all the fields regardless? Is it still worth doing assuming that the rest of the code will call the new methods idiomatically? Could you prevent using the direct struct making by adding a private field with the () type? Is there any value in that?

pub fn new(damage: f32) -> Damage {

@marotili
Copy link
Contributor

marotili commented Jun 5, 2019

Hi @bowbahdoe

Sorry for the late response. It is perfectly fine to ask here :)

We started out without prefabs, so we manually created the Damage struct using Damage::new. This doesn't happen anymore because the prefabs take care of creating the data.

If you want to prevent creating the structs manually, you can set their fields as private by removing the pub qualifier.

I think what makes more sense depends on your use case. If you have complex data structs, I'd hide their implementation behind new. An example would be Perspective. Perspective stores a matrix internally and you don't want the user to create that matrix manually.

I hope that helps

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants