2019년 11월 25일 월요일

The Mystery of Mutable Kotlin Collections 를 읽고

https://proandroiddev.com/the-mystery-of-mutable-kotlin-collections-e82cbf5d781

Kotlin에서 MutableList는 List는 아래와 같다.

public interface Mutable:ist<E> : List<E>, MutableCollection<E> {}
public interface List<out E> : Collection<E> {}

List: read-only access
MutableList: read/write access

- MutableListOf(...)나 listOf(...)를 통해서 만들어진 리스트는 MutableList로 인식된다.
- List 인터페이스를 Kotlin으로 직접 구현하면 MutableList로 인식하지 않는다.

위처럼 되는 이유는?
Kotlin에서의 List는 mock interface이기 때문이다.
Kotlin에서의 List는 컴파일시 사라지고 Java에서의 List로 변환된다.

댓글 없음:

댓글 쓰기

Generic interfaces 요점

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