OptionSet as Sequence


(J.E. Schotsman) #1

Hello,

Recently I wrote code like this:

let domains:[FileManager.SearchPathDomainMask] = [.userDomainMask, .localDomainMask, .networkDomainMask]
for domain in domains { … }

But I would prefer this:

let domains:FileManager.SearchPathDomainMask = [.userDomainMask,.localDomainMask, .networkDomainMask]
for domain in domains { … }

This requires this OptionSet to conform to Sequence.
Would it be possible to generically conform all OptionSets with RawValue:IntegerArithmetic to Sequence?
I’ve tried but it seems to be tricky.

Jan E.


(Zhao Xin) #2

I don't see the needs to call `for domain in domains` with `domains:OptionSet`.
If a parameter want an OptionSet, why not just use`domains` directly
instead of `domain`?

Zhaoxin

···

On Mon, Mar 13, 2017 at 12:09 AM, J.E. Schotsman via swift-users < swift-users@swift.org> wrote:

Hello,

Recently I wrote code like this:

let domains:[FileManager.SearchPathDomainMask] = [.userDomainMask,
.localDomainMask, .networkDomainMask]
for domain in domains { … }

But I would prefer this:

let domains:FileManager.SearchPathDomainMask = [.userDomainMask,.localDomainMask,
.networkDomainMask]
for domain in domains { … }

This requires this OptionSet to conform to Sequence.
Would it be possible to generically conform all OptionSets with
RawValue:IntegerArithmetic to Sequence?
I’ve tried but it seems to be tricky.

Jan E.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(J.E. Schotsman) #3

You are right, I was writing bad code.
When working with option sets you always end up writing code like

if options.contains(flag1) {...}
if options.contains(flag2) {…}
...

No need for a for-in loop.
Please ignore my posting.

Jan E.

···

On 12 Mar 2017, at 23:23, Zhao Xin <owenzx@gmail.com> wrote:

I don't see the needs to call `for domain in domains` with `domains:OptionSet`. If a parameter want an OptionSet, why not just use`domains` directly instead of `domain`?