Quantcast
Channel: When is it appropriate to use an associated type versus a generic type? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Steve Klabnik for When is it appropriate to use an associated type versus a generic type?

$
0
0

Associated types are a grouping mechanism, so they should be used when it makes sense to group types together.

The Graph trait introduced in the documentation is an example of this. You want a Graph to be generic, but once you have a specific kind of Graph, you don't want the Node or Edge types to vary anymore. A particular Graph isn't going to want to vary those types within a single implementation, and in fact, wants them to always be the same. They're grouped together, or one might even say associated.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>