Swift: creating a protocol/mixin strategy that forces implemented structures to be enums with defined RawValue

Viewed 24

Basically what i'm trying to do is to create a windows-based role representation inside a Swift program. My thoughts are as simple as to create a protocol (and apply mixin strategies if needed) to force inheriters to be a enum with String RawValue.

protocol Role {
    var rawValue: String { get }
}
extension Role: RawRepresentible<RawValue: String> { }

// or

protocol Role: RawRepresentible where RawValue == String { }

and to be used something like:

enum BusinessRole: Role, Codable {
    case bronzeAccess = "BRONZE_ACCESS"
    case premiumAccess = "PREMIUM_ACCESS" 
    case platinumAccess = "PLATINUM_ACCESS"
}

enum PaymentRole: Role, Codable {
    case creditCard
    case wallet
}

var roles: [Role] = allRoles()
let canPayWithCreditCard = roles.contains(.creditCard)

But no matter what i do, I cant find a way to use the Role as a type because of errors like Protocol 'Role' can only be used as a generic constraint because it has Self or associated type requirements; i dont think it should be this hard but I can't seem to solve it.

0 Answers
Related