Skip to content
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

Associated items #5445

Open
ArielElp opened this issue Apr 18, 2024 · 0 comments
Open

Associated items #5445

ArielElp opened this issue Apr 18, 2024 · 0 comments
Labels
large roadmap a feature included in the public roadmap

Comments

@ArielElp
Copy link
Collaborator

Similarly to Rust, we want to add the notion of associated items to Cairo. In particular, associated types, which are a blocker for writing iterators the "right way" (and thus a blocker to nice for loops):

pub trait Iterator<C> {
    type Item;
    fn next(ref self: C) -> Option<Self::Item>;
}

The next in the line of associated items to be implemented is associated consts. For a natural usecase example, consider game characters where each character has a specific amount of hit points. You don't want the character type to be generic on the hitpoints since this would allow instantiating characters with the wrong values. Instead, with associated consts, you can define the following trait:

trait Character<CharType> {
   const MAX_HEALTH: u32;
   fn heal(ref self: CharType);
}

struct Wizard {
   health: u32
}

impl Gandalf of Character<Wizard> {
   const MAX_HEALTH: u32 = 100;
   fn heal(ref self: Wizard) {
      self.health = Self::MAX_HEALTH;
   }
}
@ArielElp ArielElp added large roadmap a feature included in the public roadmap labels Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
large roadmap a feature included in the public roadmap
Projects
Status: v2.7.0(upcoming)
Development

No branches or pull requests

1 participant