Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
공변성, 반공변성(책에서는 반변covariance으로 표현)에 대해 개념은 알고 있었지만 제가 정확히 몰랐던 부분이 있었습니다.
제가 즐겨서 생성하고 선언하는 List 제네릭 클래스는 다음과 같습니다.
저는 이 코드를 알기 때문에 공변성이다! 라고만 생각했고 여태까지 그렇게 알고 있었습니다.
첫번째,
책의 설명대로라면 읽기 전용 인터페이스이기 때문에 여태 A 타입의 서브타입인 B가 왜 가능한지 설명하지는 못했습니다.
하지만 변경할 수 있는 인터페이스(예: IList와 같은)는 가능하지 않으므로 공변성이 무엇인지 더 정확하게 알게 되었습니다.
이것 때문에 IDE에 코드 작성을 꽤 오래 해봤습니다.
두번째, 제가 알고 있는 코드는 제네릭 타입에 대한 것 뿐이고 매개변수 타입을 더 따져보지는 못했습니다.
책을 읽고 제네릭 타입과 제네릭 매개변수 타입에 대해 더 따져봐야 하고,
그래야 입력(in)에 대한 매개변수일 때 반공변성이 성립하고 이에 대해 더 정확히 이해할 수 있게 되었다는 걸 알았습니다.
이제 어디가서 공변성에 대해서만 설명하고 반공변성을 설명해 주지 못했는데
이제 확실히 알고 설명할 수 있게 된 좋은 계기가 아닌가 하는 생각이 듭니다.