[Swift] marker protocol 이 뭔디요 (feat. Sendable)
GPT 가 덜 알려줘서 인간이 좀 더 알아보기
들어가기 전에
애플문서 보다보면 이곳저곳 Sendable
이 marker protocol 이라는 말이 나옵니다??
근데 이게 뭔지,, 제가 알리가 없져?? ㅋㅋ 바로 검색 ㄱ
marker protocol
지피티와 What is a “marker protocol” in Swift? Stack over flow 답변은 요 정도로 정리됩니다
marker protocol 은 메서드나 프로퍼티 등의 요구 사항이 없는 프로토콜
protocol Marker {}
하지만 뭔가 찜찜하죠..?
🤔 이게 끝인데 marker protocol 이라는 단어가 문서에 계속 나온다고?? 뭐 더 있는거 아니야?
결국 좀 더 구글링하니까 SE-0302 문서에 나와있더라구여 ㅎ
Swift 에서 marker protocol 은, @_marker
attribute 가 붙은 protocol 입니다
@_marker protocol Marker { }
이 프로토콜은 컴파일 타임에서만 영향을 미치는 개념인데 (런타임에는 아무런 영향 X), 이 attribute 가 붙게 되면 아래와 같은 제한이 생깁니다
- 어떠한 요구사항도 가질 수 없음
@_marker
protocol Marker {
func hello()// ❌ Marker protocol 'Marker' cannot have any requirements
}
- 마커 프로토콜이 아닌 프로토콜을 상속할 수 없음
protocol MySuperProtocol {}
@_marker
protocol Marker: MySuperProtocol { // ❌ Marker protocol 'Marker' cannot inherit non-marker protocol 'MySuperProtocol'
}
is
또는as?
에서 타입으로 사용할 수 없음
@_marker
protocol Marker { }
class MyMarkerClass: Marker { }
func checkIsMarker() {
let myMarkerClas = MyMarkerClass()
myMarkerClas is Marker // ❌ Marker protocol 'Marker' cannot be used in a conditional cast
myMarkerClas as? Marker != nil // ❌ Marker protocol 'Marker' cannot be used in a conditional cast
}
- 비(非)마커 프로토콜에 대한 조건부 프로토콜 적합성을 위한 제네릭 제약으로 사용할 수 없음 (A marker protocol cannot be used in a generic constraint for a conditional protocol conformance to a non-marker protocol)
@_marker
protocol Marker { }
protocol ElementPresentable {
func display()
}
extension Array: ElementPresentable where Element: Marker { // ❌ Conditional conformance to non-marker protocol 'ElementPresentable' cannot depend on conformance of 'Element' to marker protocol 'Marker'
func display() { }
}
사실 요 마지막 코드는 해석을 잘 못해서.. 맞는 예시인지 모르겠는데,,? 컴파일 에러가 비슷하게 나오는거 보니까 맞는걸까요? 아니라면 알려주시면 감사리~
Marker protocol — Sendable
위에서 살펴본 @_marker
attribute 는 사실 prefix 로 _
가 붙은걸로 봐서.. 아직 우리가 직접 사용하는건 권장하지 않는듯합니다.
SE-0302 제안서에도 요렇게 써있구요
We think this is a generally useful feature, but believe it should be a compiler-internal feature at this point.
지금 시점의 swift 에서는 단 하나!의 marker protocol 을 가지고 있는데, 그게 바로 Sendable
입니당 (출처 — _marker)
사실 Sendable
선언부를 봤을때 @_marker
이 없어서 오잉? 싶었지만
튼 markable protocol 이라니까,, 이렇게 캐스팅하면 안된답니다?? (어쩐지 시도해봤을때 에러나더라니ㅎ)
그렇담 적용된 @_marker
attribute 는 우리가 못보게 막혀있는걸까요?? 뭐징
마무리
ㅇㅋ 이건 지피티가 덜 알려줬죠? 그래서 메모할 겸 남겨두기~~
문서 보다가 링크 텍스트가 빠져있길래 pr 도 남겨뒀서여
그럼