Skip to content

Instantly share code, notes, and snippets.

@Kametrixom
Last active September 3, 2015 07:03
Show Gist options
  • Save Kametrixom/437a607e16eacf08e9c1 to your computer and use it in GitHub Desktop.
Save Kametrixom/437a607e16eacf08e9c1 to your computer and use it in GitHub Desktop.
// Make this code compile without changing these lines
func raw<T: RawRepresentable>(t: T) -> T.RawValue {
return t.rawValue
}
let array : [E] = [.A(true), .B(1), .C("C")]
array.map(raw)
if case .C(let s?)? = array.first {}
// If you want a bigger challenge, include this part:
for elem in array {
switch elem {
case .C(_):
print("C!")
}
}
@kostiakoval
Copy link

No full challenge :)

enum E: RawRepresentable {
  case A(Bool)
  case B(Int)
  case C(String?)

  init?(rawValue: Any) {

    switch rawValue {
    case let a as Bool:
      self = .A(a)
    case let b as Int:
      self = .B(b)
    case let c as String:
      self = .C(c)
    default:
      return nil
    }
  }

  var rawValue: Any {
    switch self {
    case A (let a) : return a
    case B (let b) : return b
    case C (let c) : return c
    }
  }
}

@pyrtsa
Copy link

pyrtsa commented Aug 31, 2015

enum E : RawRepresentable {
    case C(String?)

    init?(rawValue: String) {
        self = .C(rawValue)
    }

    static func A(x: Bool) -> E {
        return .C(String(x))
    }

    static func B(x: Int) -> E {
        return .C(String(x))
    }

    var rawValue: String {
        switch self {
        case let .C(value): return value ?? ""
        }
    }
}

@Kametrixom
Copy link
Author

Congrats to @kostiakoval for solving the challenge when there wasn't an extended version yet
Congrats to @pyrtsa for solving the extended challenge ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment