I'm trying to write a helper function which will convert an array of bit indexes to a class conforming to OptionSet.
func getOptionSet<T: OptionSet>(bitIndexes: [Int64]) -> T {
var result: Int64 = 0
for index in bitIndexes {
result |= 1 << index
}
return T(rawValue: result) // error
}
This fails to compile:
Cannot invoke initializer for type 'T' with an argument list of type '(rawValue: Int64)'
I've also tried using RawValue:
func getOptionSet<T: OptionSet>(bitIndexes: [T.RawValue]) {
var result = T.RawValue() // error
This doesn't work as well:
Cannot invoke value of type 'T.RawValue.Type' with argument list '()'
Can this be done? Do I need to add additional constraints on T?
I know it's possible to rewrite this function to use a concrete type, but I want to keep it generic if possible.