What's the Diff: BoxView<SelectionValue: Hashable>: View vs. BoxView<SelectionValue>: View where SelectionValue: Hashable

struct BoxView<SelectionValue: Hashable>: View {


struct BoxView<SelectionValue>: View where SelectionValue: Hashable {

?? Both compile just fine in this case. But sometime I think I have to use where version. What's the different in these two? When to use which? Why Swift don't just allow only one style?

1 Like

There is no semantic difference.

The top one predates the bottom one, but could not express as many things, such as constrained associated types (Something<T: Collection> where T.Element: Hashable). The older syntax was likely kept around simply for backward compatibility. Or possibly because it is shorter where it does work.


Your choice. Some developers use only the second. Others use the first in every situation where it is sufficient, and only resort to the second when necessary.


Ok, I see now. The simpler form is easier to read for me. I will stick to the simpler form and use where only when needed.

Terms of Service

Privacy Policy

Cookie Policy