Given I want to use a data-oriented approach I'm using this code:
pub struct Store {
pub players: BTreeMap<uuid::Uuid, Player>,
pub coach_by_player_id: HashMap<uuid::Uuid, uuid::Uuid>,
pub skills_by_player_id: HashMap<uuid::Uuid, Vec<uuid::Uuid>>,
}
instead of using the OOP approach with struct like:
pub struct Player {
id: uuid::Uuid,
// other fields here
coach: Box<Coach>
skills: Vec<Skill>
}
An issue I'm having is that sometimes I forget to push some object to the store but that object is needed to create another object that needs to read it from the store, like this example:
let store = &mut Store::default();
let team = Team::new();
let player = Player::new(team.id, "Bob", store);
impl Player {
pub fn new(
team_id: uuid:Uuid,
name: &str,
store: &mut Store,
) -> Self {
if let Some(team) = store.get_team(team_id) {
// use team here
};
// create player here
}
}
Since I forgot to push the team
before creating the new player
I get an error at run-time and not at compile-time.
How can I have a compile-time error in cases like this?
I need a way to find out at compile-time
that I do not pushed an object in the store.