Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item 31. 한정적 와일드카드를 사용해 API 유연성을 높이라 #87

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

seizze
Copy link
Member

@seizze seizze commented Jan 23, 2021

자바 제네릭 불공변으로 인한 API 제약을 해결하는 한정적 와일드카드 타입에 대해 요약하고, Objective-C에서의 __covariant__contravariant를 소개하였습니다.
스위프트에서의 공변/불공변에 대해 정리하고 한정적 와일드카드 타입과 유사한 기능을 구현할 수 있는 방식과 그 한계에 대한 내용, 그리고 스위프트에서 공변인 경우에 대한 설명을 추가하였습니다.

Closes #67

@seizze seizze self-assigned this Jan 23, 2021
Copy link
Member

@gwonii gwonii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

덕분에 Type Erasure에 대해 학습할 수 있는 좋은 기회였습니다! 고생하셨습니다~~

MAnySequence.make([1, 2, 3, 4, 5]).forEach { print($0) }
MAnySequence.make([1 ..< 4]).forEach { print($0) }
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type Erasure 로 직접 타입을 랩핑하는 경우 대신 Opaque 타입을 통해 컴파일 타임에 에러를 안나게 하는 방법도 있더군요..

그래서 Opaque 타입을 가지고 이것저것 해봤는데
일반 메소드의 return 타입이나 properyt에는 사용할 수 있는데
매개변수나 clousure의 return 값으로는 사용할 수가 없네요.
혹시 Opapue type에 대해서는 어떻게 생각하시는지요~~~~?

@ehgud0670 ehgud0670 removed the Gwonii label May 2, 2021
Copy link
Member

@ehgud0670 ehgud0670 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매우 잘 읽었습니다!!

  • 스위프트는 안정성을 위해서 일부러 제한시킨건지 아님 아직 안나온건지 지켜봐야 겠네요 ㅎㅎ Objective-C * __covariant와 __contravariant 도 잘 읽었습니다.
    고생하셨습니다..! 💯💪

func put<C: Car>(in garage: Garage<C>) { ... }

put(in: Garage<Car>())
put(in: Garage<PoliceCar>()) // 가능
Copy link
Member

@ehgud0670 ehgud0670 May 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 제네릭을 메소드에 적용했기 때문에 컴파일 통과한거고, 결국엔 자바의 한정적 와일드 카드의 역할은 못하는거군요?!

이렇게 작성하면 컴파일에러가 뜨더라고요.

class Stack<E: Car> {
    func push(_ e: Garage<E>) { }
}

abc.push(Garage<Car>())
abc.push(Garage<PoliceCar>()) // 컴파일 에러! Cannot convert value of type 'Garage<PoliceCar>' to expected argument type 'Garage<Car>'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 그렇네요 type constraint도 메서드 범위 내에서만 적용 가능한가봅니다

@ehgud0670 ehgud0670 removed the Jason label May 2, 2021
Copy link
Member

@delmaSong delmaSong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스위프트의 커스텀타입의 제너릭이 불공변이고 배열은 공변이라는 점을 새롭게 알아가네요!
수고많으셨습니다 😃

func put<C: Car>(in garage: Garage<C>) { ... }

put(in: Garage<Car>())
put(in: Garage<PoliceCar>()) // 가능
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 그렇네요 type constraint도 메서드 범위 내에서만 적용 가능한가봅니다

@delmaSong delmaSong removed the Delma label May 30, 2021
Copy link
Member

@dev-Lena dev-Lena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한정적 와일드카드에 대한 글 잘 읽었습니다. 어려운 내용이었는데 정말 고생하셨습니다!! 👏🏻 최고! 👍🏻
(저는 더 찾아보고 공부하면서 이해해야겠네요 🙄 ㅎㅎ)

@dev-Lena dev-Lena removed the Lena label May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Item 31. 한정적 와일드카드를 사용해 API 유연성을 높이라
5 participants