Variadic convenience initialiser
Closed this issue · 5 comments
Benefits:
- Cleaner notation for literal sets (e.g.,
Set(1, 2, 3)
) - Side-benefit of tidier singleton sets (e.g.,
Set(1)
)
Cautious 👍
I like the idea; the caution is due to the potential for ambiguity with other init
forms. I think we should be ok, even for the case of singleton sets of arrays: Set([1, 2, 3])
would probably mean Set<Int>(1, 2, 3)
and not Set<[Int]>([ [1, 2, 3] ])
. But it’s worth testing thoroughly.
(And being able to parameterize Set
by the specific type implies an escape hatch if there are problems.)
Ok, so Set<[Int]>([ [1, 2, 3] ])
actually doesn't even compile at the moment, because Array
doesn't conform to Hashable
! Interestingly though, Set([[1, 2, 3]])
does compile—but only in the presence of Foundation, because its type is inferred as Set<NSArray>
.
Thus, I haven't been able to discover any case of surprising type inference yet. Hoping to open a PR to that effect soon.
Aha, good catch ✨