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

타입으로 견고하게 다형성으로 유연하게 4주차 - 이정안 #478

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fkdl0048
Copy link
Contributor

개인적으로 C#의 IEnumerable에 대해서 공부할 때 해당 내용을 정리한 경험이 있어서 잘 이해가 된 것 같습니다. 당시에도 어렵게 이해한 내용이 많았고, C# 키워드에서도 in, out키워드가 존재해서 좀 더 쉽게 이해했습니다!

Copy link

우측에 있는 Projects, Milestone, Development를 확인 후 할당 해주세요~! 🙇

Copy link
Member

@jongfeel jongfeel left a comment

Choose a reason for hiding this comment

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

👍


### 논의사항

저는 가변성 부분에서 IEnumerable이나 C#의 제네릭 델리게이트 타입인 Func이 생각이 났습니다. 아마 제가 자주 사용해서 그런 것 같은데 다른 언어에도 이렇게 사전에 정의되어 있는 제네릭 델리게이트가 있는지 궁금합니다. 책에서는 함수 타입이라고 하는 것 같습니다.
Copy link
Member

Choose a reason for hiding this comment

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

자바에도 있습니다. 그 예제에 대해서 @ymkim97 님이 간단하게 설명해 주셨던 것 같아요.
하지만 delegate는 없기 때문에 제네릭 클래스와 사용 방법이 같을 겁니다.
그 외에 타입스크립트는 잠깐 검색해 보니 같은 특징을 모두 가지고 있는 것 같습니다.

Copy link
Member

@jongfeel jongfeel Feb 21, 2025

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 파라미터라 반공변이라서 이제 설명할 수 있습니다.

Copy link
Member

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>'

Copy link
Member

@jongfeel jongfeel left a comment

Choose a reason for hiding this comment

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

공변, 반공변에 대한 내용을 하나 더 추가했습니다.


### 논의사항

저는 가변성 부분에서 IEnumerable이나 C#의 제네릭 델리게이트 타입인 Func이 생각이 났습니다. 아마 제가 자주 사용해서 그런 것 같은데 다른 언어에도 이렇게 사전에 정의되어 있는 제네릭 델리게이트가 있는지 궁금합니다. 책에서는 함수 타입이라고 하는 것 같습니다.
Copy link
Member

@jongfeel jongfeel Feb 21, 2025

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 파라미터라 반공변이라서 이제 설명할 수 있습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2025 타입으로 견고하게 다형성으로 유연하게 탄탄한 개발을 위한 씨줄과 날줄
Projects
Status: In review
5 participants