Protocol
2022 WWDC Design protocol interfaces in Swift
Understand type eraure: associated type이 있는 protocol이 existential any type과 어떻게 상호 작용하는지 설명. Hide implementation details: interface와 implementation을 분리해서 캡슐화하기 위해 opaque type을 사용하는 방법에 대해 설명. Identify type releationships: protocol에서 same-type requirements가 concrete type의 여러가지 다른 sets간의 관계를 모델링하는 방법에 대해 설명. Understand type eraure protocol Animal { associatedtype CommodityType: Food func produce()..
Existential any in Swift explained with code examples(번역)
원문: https://www.avanderlee.com/swift/existential-any/ Existential any in Swift explained with code examples Existentials in Swift are defined using the any keyword and provide dynamic types, but also come with performance impact you should know. www.avanderlee.com Existential any를 사용하면 type 앞에 any 키워드를 붙여 existential types를 정의할 수 있다. 간단히 말해 existential는 “any type이지만 protocol ’something’을 준수한다.”를..
Protocol Optional Function
Swift의 프로토콜을 보면 Optional Funcion 이라는 것이 있다. 말 그대로 구현해도 되고 구현하지 않아도 되는 함수라는 뜻이다. protocol Dog { func bark() } extension Dog { func bark() { print("Bow-wow") } } final class Samoyed: Dog { init() { bark() } } Samoyed() 위 코드를 실행하면 “Bow-wow”를 출력하는 것을 볼 수 있다. 그런데 protocol은 구현체가 아닌데 어떻게 이게 가능할까? Protocols can be extended to provide method, initializer, subscript, and computed property implementations ..