Hacker News new | past | comments | ask | show | jobs | submit login

It would allow you to call a function to check for trait B and downcast to "implements trait A and B" in the case that it does implement the trait.



It seems like a way to ask "Can this thing implement X and if so how?" from say the Any trait would be what you want here, I have no idea how hard that would be to deliver but I also don't see how the previous trait thing is relevant, like, why do we need to say up front that maybe we will care whether trait B is implemented?


I’m still learning the language but couldn’t you use an enum containing two types to accomplish the same thing?


You can if you know all of the possible types in advance. But if you want to expose this as an interface from a library that allows users to provide their own custom implementation then you need to use traits.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: