• 0 Posts
  • 7 Comments
Joined 2 years ago
cake
Cake day: July 10th, 2023

help-circle

  • Basically, you can generalize your trait types into their parent (super) traits for situations when functionality is specific to those supertrait objects.

    As an example, if you have a trait CanBark and it is a super trait for the trait IsDog, you can coerce your references of &dyn IsDog into a &dyn CanBark. You can then work with other trait types that share a super trait.

    trait CanBark {
        fn bark(&self);
    }
    trait IsSeal: CanBark { }
    trait IsDog: CanBark { }
    
    fn bark_as_group(barkers: &Vec<&dyn CanBark>) {
        for barker in barkers {
            barker.bark();
        }
    }
    
    let spot: &dyn IsDog = get_spot();
    let seal: &dyn IsSeal = get_seal();
    let barkers: Vec<&dyn CanBark> = Vec::new();
    barkers.push(spot);  // coerced
    barkers.push(seal);  // coerced
    bark_as_group(&barkers);
    

    At least, I hope this is possible now. If it’s purely “you can return a coerced type from a function”, that is less useful.





  • I’ve found that one of the best things to do when making a library for something that is going to have a web interface is to first have it work in the terminal. You can much more quickly play around with the design and fix issues there instead of having to work with a more complex web interface.

    You just create a simple menu system, like input("1: Feature A\n2: Feature B\n>") and just start trying out all of the different scenarios and workflows.