Skip Navigation

User banner
Anselm "Two Sheds" Schüler
Anselm "Two Sheds" Schüler @ anselmschueler @ieji.de
Posts
0
Comments
4
Joined
2 yr. ago

  • @capuccino

    Then, when you write somewhere that some type satisfies a trait, like "T: Foo", and Foo has an associated type, you can further specify that not only does T need to implement Foo, but that the associated type satisfies some criteria.

    You can do this two ways:
    "FooBar = Something" says it has to be something specific,
    "FooBar: Baz" says that it has to implement another trait

  • @capuccino

    A trait can have an associated type. You write this "trait Foo { type Bar; }". Then, when you implement the trait, you have to specify this type, with "type Bar = Something;". This is different from the trait itself being generic, because there can only be a single associated type, so you can't implement the trait multiple times with different associated types.

  • @capuccino An alternative is to not use T: AddOutput = T, but to replace your usage of T, where it represents the output type, with T as Add::Output

  • @capuccino You're not assigning anything. The Output = T is expressing a constraint. It's saying that the addition of T has to result in T again. A type can implement Add with any output type it wants.