Quite often I use map or flatMap on a Set and need a Set as result again. Both methods return an Array though.
While it is quite easy to turn an Array back into a Set using its initializer init<S>(_ sequence: S) it is still a bit inconvenient.
There are a few questions though: map and flatMap might both return elements that are not Hashable which is a requirement to put them into a Set.
Would it make sense to use Conditional Conformance here and have both methods only return a Set if the result actually can be a Set? Would that be confusing to the developer and/or hard for the type-checker?
Am I missing something obvious here? Maybe all of that is a bad idea after all and there is a better solution?
Thank you in advance for your input.
Notice that if map(_:) returned a Set this would not necessarily hold true. If this isn't the behavior you're looking for, you're always free to pass the result through the sequence initializer.
map is the name of a higher-order function that applies a given function to each element of a list, returning a list of results in the same order
map is usually "defined" to be "apply a function on each element in a list", which would make changing the size of the list during the operation highly surprising.
filter can get away with this because doesn't need to change the type of Element. map and flatMapdo need to change the type of Element so they would need a type system feature that doesn't exist yet (highed-kinder types).