1

In this sample code, Self.self doesn't behave as expected. Depending on the type of the parameter chosen, the identifier property has not the same value.

protocol Identifiable : class {
    static var identifier: String { get }
}

extension Identifiable {
    static var identifier: String {
        return String(describing: Self.self)
    }
}

class Animal : Identifiable {}
class Tiger : Animal {}

Animal.identifier // Animal
Tiger.identifier // Tiger

func identifiableIdentifier<T: Identifiable>(of type: T.Type) -> String {
    type // Tiger.Type
    return type.identifier
}

identifiableIdentifier(of: Tiger.self) // Animal 


func animalIdentifier<T: Animal>(for type: T.Type) -> String {
    type // Tiger.Type
    return type.identifier
}

animalIdentifier(for: Tiger.self) // Tiger

Does someone know why ?

GaétanZ
  • 4,870
  • 1
  • 23
  • 30
  • 2
    Compare https://stackoverflow.com/q/42037852/2976878. `Self` is a weird and (not so) wonderful type in a protocol extension; you're better off just using `self` in your case. – Hamish Jan 23 '18 at 15:16
  • Thanks @Hamish, it is hard to find the right words on Google – GaétanZ Jan 23 '18 at 17:26

0 Answers0