Is this something that Silver will support eventually? I know I can produce similar behavior using a Set right now but OptionSet offers extra convenience.
can you elaborate?
OptionSet behaves like a set but defines its own set elements. It is designed for bitsets and is quite handy.
Link to official Swift documentation: https://developer.apple.com/documentation/swift/optionset
Ah, I see. you feel like contributing it to https://github.com/remobjects/SwiftBaseLibrary, based on Set
?
That’s a possibility. I’ll see what I can do, when I have some free time.
Cool.
Probably easiest to either copy Set and adjust it or (less per-platform work) but on top of the exiting Set, either by descending or wrapping it (depending on API needs)
I’ve been experimenting a bit. This is how I’ve set things up so far (this doesn’t do anything - just trying to incrementally get it to compile). This is in a shared project (“SharedProject” in the attached test case):
public protocol OptionSet: RawRepresentable {
associatedtype Element = Self
}
This is in an iOS application project:
public struct Flags: OptionSet {
typealias RawValue = UInt
let rawValue: UInt
init(rawValue: UInt) {
self.rawValue = rawValue
}
public static let flag1 = Flags(rawValue: 1 << 0)
public static let flag2 = Flags(rawValue: 1 << 1)
public static let flag3 = Flags(rawValue: 1 << 2)
public static let flag4 = Flags(rawValue: 1 << 3)
public static let flag5 = Flags(rawValue: 1 << 4)
public static let flag6 = Flags(rawValue: 1 << 5)
}
The second snippet is identical to what I had in my Xcode/Swift app’s source code (plus the typealias
line, since it wasn’t required).
Here’s what I’m getting when compiling:
I have attached this minimalist test case below:
TestOptionSet.zip (157.5 KB)
Hmm, this seems like a bug, but I think its getting confused because RawRepresentable
seems to be generic, but you’re declaring to implement RawRepresentable, not RawRepresentable. I’m not sure.
protocol IRawRepresentable<id!>! {
init(`init` rawValue: RawValue)
let rawValue: id { get }
}
let rawValue: UInt
init(rawValue: UInt) {
self.rawValue = rawValue
}
these should match up, for for some reason they don’t…
Thanks, logged as bugs://81777