Skip to content

Feature: Primary vs secondary inheritance #85

@tristanpoland

Description

@tristanpoland
// We should try to see if its feasible to allow
pub struct Throwable {
    health: i32,
}

pub struct Enemy {
    health: i32,
}


#[inherit(Primary(Throwable), Secondaries([Enemy, ...]))]
pub struct Turtle {

}

impl Turtle {
    pub fn new() -> Self {
        Self {
            health: 100,
        }
    }

    fn damage(&mut self, amount: i32) {
        self.health -= amount; // We can assume this is the health from Throwable, as it's the primary parent.
        println!("Turtle took {} damage, health is now {}", amount, self.health);
    }
}

/// As opposed to no primary, where we would have to specify which health we are referring to (Throwable or Enemy), we can directly access it here without ambiguity.

pub struct Throwable {
    health: i32,
}

pub struct Enemy {
    health: i32,
}


#[inherit(Throwable, Enemy)]
pub struct Turtle {

}

impl Turtle {
    pub fn new() -> Self {
        Self {
            health: 100,
        }
    }

    fn damage(&mut self, amount: i32) {
        self.throwable.health -= amount; // We can assume this is the health from Throwable, as it's the primary parent.
        println!("Turtle took {} damage, health is now {}", amount, self.throwable.health);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions