2017년 12월 12일 화요일

Swift 스터디 - Associated Types Versus Generics

https://doc.rust-lang.org/book/second-edition/ch19-03-advanced-traits.html

https://www.youtube.com/watch?v=XWoNjiSPqI8

http://www.russbishop.net/swift-associated-types

https://www.bignerdranch.com/blog/why-associated-type-requirements-become-generic-constraints/

https://pdfs.semanticscholar.org/30c6/1a232dbf78a1720b06946eab0c9cb0a645b2.pdf

type parameter가 늘어날수록 사용이 불편해진다.
Generic은 public interface에 포함된다. 따라서, type이  open된다.
type parameter가 여러개일 경우 이중 하나에만 특정지을 수 없다.(나머지도 항상 표시가 필요하다.)
새로운 type parameter를 추가하면 이전 코드가 깨진다.
type parameter로 표현하는 경우 SomeProtocol<String>과 SomeProtocol<UITableViewCell>처럼 여러 타입에 대한 구현을 가질 수 있다. 따라서, 프로토콜에서 관련 함수를 호출하려면 타입 캐스팅등이 필요하게 된다.

associatedtype을 사용하는 protocol은 generic constraint와 함께 사용된다. 그렇지 않을 경우 아래와 같은 에러 메시지를 보게 된다.
  "Protocol `SomeProtocol` can only be used as a generic constraint because it has Self or associated type requirements"

Covariant Parameter Types

function return values can changed to subtypes, moving down the hierarchy, whereas function parameters can be changed to supertypes, moving up the hierarchy

https://www.java-tips.org/java-se-tips-100019/24-java-lang/482-covariant-parameter-types.html

https://softwareengineering.stackexchange.com/questions/267310/overriding-methods-by-passing-as-argument-the-subclass-object-where-the-supertyp

https://mikeash.com/pyblog/friday-qa-2015-11-20-covariance-and-contravariance.html

Type Erasure

https://krakendev.io/blog/generic-protocols-and-their-shortcomings

https://www.bignerdranch.com/blog/breaking-down-type-erasure-in-swift/

https://www.slacktime.org/type-erasure/

https://academy.realm.io/posts/tryswift-gwendolyn-weston-type-erasure/

Generic interfaces 요점

 https://go.dev/blog/generic-interfaces  Generic interface를 정의할 때 최소한의 제약만을 정의하고 실제 구현체들이 자신만의 필요한 제약을 추가할 수 있도록 하는 것이 좋다. pointer receiver를...