amethyst / evoli

An ecosystem-simulation game made with Amethyst
https://community.amethyst.rs/t/evoli-introduction/770
Other
217 stars 33 forks source link

Why the new methods? #78

Open bowbahdoe opened 5 years ago

bowbahdoe commented 5 years ago

(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?

https://github.com/amethyst/evoli/blob/efaf66dbfc0c40b607a0d3d8014c5a7178e0985f/src/components/combat.rs#L47

marotili commented 5 years ago

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