0

I have a trait that implements another trait:

trait RandomAccessIterator : Sub + VariousOthers {}

How do I specify, that for all implementations of this trait, the result of the subtraction (the Output type within Sub) must be of a certain type, such as isize? That way, if I write a generic function which uses objects implementing this trait, I know (and more importantly, the compiler knows) that the result of A - B is type isize.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • 1
    Does `trait RandomAccessIterator : Sub + VariousOthers + Sized {}` not work? You didn't provide any code that would show how *what you have already tried* fails. I'd expect more from someone with that 70k reputation :-(. – Shepmaster Sep 15 '16 at 17:15
  • @Shepmaster: Yes, that works. Thank you. I'm not sure how showing my failed attempts would help you answer the question. This is a syntax that I was not previously aware of. I could throw a hundred guesses at what the syntax might be. How would that help you? – Benjamin Lindley Sep 15 '16 at 17:21
  • 3
    The failed attempts would presumably have had code that would start working when the requirement was successfully implemented. That would have been useful to validate that my guess actually worked. Besides, [it's expected that you show that you've expended effort before asking a question](http://meta.stackoverflow.com/q/261592/155423). That includes your own code attempts, but also what searching you've done here on SO and elsewhere on the Internet. Without all that, questions devolve to "code broken. you fix?" See also: [MCVE]. – Shepmaster Sep 15 '16 at 17:30
  • @Shepmaster: *"but also what searching you've done here on SO and elsewhere on the Internet"* -- I find the above information to be completely useless when I'm answering questions. Suffice it to say, you and I disagree on what makes a good question. And we're not likely to come to an agreement here. Anyway, thanks for answering my question. – Benjamin Lindley Sep 15 '16 at 17:36

1 Answers1

2
trait RandomAccessIterator : Sub<Output = isize> + VariousOthers {}

As discussed in The Rust Programming Language chapter about associated types in the section for trait objects with associated types:

The N=Node syntax allows us to provide a concrete type, Node, for the N type parameter. Same with E=Edge. If we didn’t provide this constraint, we couldn’t be sure which impl to match this trait object to.

While this isn't a trait object, the same syntax applies. Most people run into this when using operators like Mul,

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366