-
Notifications
You must be signed in to change notification settings - Fork 5
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
타입으로 견고하게 다형성으로 유연하게 4주차 - 이정안 #478
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uD0C0\uC785\uC73C\uB85C-\uACAC\uACE0\uD558\uAC8C-\uB2E4\uD615\uC131\uC73C\uB85C-\uC720\uC5F0\uD558\uAC8C-4\uC8FC\uCC28---\uC774\uC815\uC548"
Conversation
우측에 있는 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
|
||
### 논의사항 | ||
|
||
저는 가변성 부분에서 IEnumerable이나 C#의 제네릭 델리게이트 타입인 Func이 생각이 났습니다. 아마 제가 자주 사용해서 그런 것 같은데 다른 언어에도 이렇게 사전에 정의되어 있는 제네릭 델리게이트가 있는지 궁금합니다. 책에서는 함수 타입이라고 하는 것 같습니다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자바에도 있습니다. 그 예제에 대해서 @ymkim97 님이 간단하게 설명해 주셨던 것 같아요.
하지만 delegate는 없기 때문에 제네릭 클래스와 사용 방법이 같을 겁니다.
그 외에 타입스크립트는 잠깐 검색해 보니 같은 특징을 모두 가지고 있는 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추가로 제가 몇 년 전에도 아래 코드가 왜 같다고 판단 못하는거지? 라는 의문을 많이 가지고 있었는데
이번에 책 읽고 또 정확하게 알게 됐습니다.
Func<int, bool> func1;
Predicate<int> func2;
func1 = value1 = true;
func2 = value2 => false;
func1 = func2 // compile error
이걸 공식 문서 정의를 보면 다음과 같긴 합니다.
Func< T, TResult >
Predicate< T >
그래서 이 책을 읽기 전 지식으로는 Func의 TResult나 Predicate T나 타입이 같으면 같다고 봐야 하는게 아닌가? 였는데
이제 이 책을 읽고 난 후 TResult는 out parameter가 공변이고 T는 in 파라미터라 반공변이라서 이제 설명할 수 있습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
에러가 다음과 같이 나오는데 애초에 타입 자체가 안맞다... 로 표현하네요.
시그니처는 같아도 Func와 Predicate는 다른 타입이다라고 해석하는 것 같습니다.
error CS0029: Cannot implicitly convert type 'System.Predicate' to 'System.Func<int, bool>'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
공변, 반공변에 대한 내용을 하나 더 추가했습니다.
|
||
### 논의사항 | ||
|
||
저는 가변성 부분에서 IEnumerable이나 C#의 제네릭 델리게이트 타입인 Func이 생각이 났습니다. 아마 제가 자주 사용해서 그런 것 같은데 다른 언어에도 이렇게 사전에 정의되어 있는 제네릭 델리게이트가 있는지 궁금합니다. 책에서는 함수 타입이라고 하는 것 같습니다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추가로 제가 몇 년 전에도 아래 코드가 왜 같다고 판단 못하는거지? 라는 의문을 많이 가지고 있었는데
이번에 책 읽고 또 정확하게 알게 됐습니다.
Func<int, bool> func1;
Predicate<int> func2;
func1 = value1 = true;
func2 = value2 => false;
func1 = func2 // compile error
이걸 공식 문서 정의를 보면 다음과 같긴 합니다.
Func< T, TResult >
Predicate< T >
그래서 이 책을 읽기 전 지식으로는 Func의 TResult나 Predicate T나 타입이 같으면 같다고 봐야 하는게 아닌가? 였는데
이제 이 책을 읽고 난 후 TResult는 out parameter가 공변이고 T는 in 파라미터라 반공변이라서 이제 설명할 수 있습니다.
개인적으로
C#
의 IEnumerable에 대해서 공부할 때 해당 내용을 정리한 경험이 있어서 잘 이해가 된 것 같습니다. 당시에도 어렵게 이해한 내용이 많았고, C# 키워드에서도in
,out
키워드가 존재해서 좀 더 쉽게 이해했습니다!