-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Fixed types-variance.md contravariant example code comment #2938
base: main
Are you sure you want to change the base?
Conversation
Fixed the second code comment for the last example just before the summary. The words "Book" and "Buyable" needed to be swapped. This makes it consistent with how the first comment uses the term "expected", where "expected" refers to the type you see in the following line of code. The first code comment is referring to the return type in the next line of code. The second comment is referring to the parameter type in the following line of code. Therefore, in the second code comment, "Book" is what is "expected" (by the h type), and "Buyable" is what is "provided" by the f parameter type.
If I am mistaken about how these terms are used, then the first code comment needs to be changed. Either way, the current code comments are inconsistent and confusing. The way they currently read sounds like it is applying covariant logic to both explanations, even though the parameter/consumer type is and should be contravariant and the return/producer type is and should be covariant. |
val f: Function[Buyable, Buyable] = b => b So it both expects and returns a |
You can try to extend the sentence with the verb used:
|
It seems to me that "expected" should consistently refer either a type in Here is the complete example with both comments:
The first comment is comparing the return type of The second comment is comparing the parameter type of I don't think swapping the meaning of "expected" between these two comments is helpful because it makes it sound like a line of reasoning rather than a statement of what is or how the language works. Then it can become confusing because it sounds like it is using covariant reasoning to defend contravariant types by always putting the more specific type first. This could imply that the reason it makes sense is because a "Buyable" is an "Item" (which intuitively makes sense), and that "Book" is a "Buyable" (which also intuitively makes sense, and thus is misleading). |
Perhaps the types should be flipped in the first comment instead of the second. Also, changing the word "OK" to "Allowed" would go even further in the direction of making these sound like statements of what is / how it works, rather than a line of reasoning. |
@joroKr21 care to respond further? |
Well I think it's precisely the variance that determines in which direction to apply "expected". Let's say we expect a type
|
Fixed the second code comment for the last example just before the summary. The words "Book" and "Buyable" needed to be swapped. This makes it consistent with how the first comment uses the term "expected", where "expected" refers to the type you see in the following line of code. The first code comment is referring to the return type in the next line of code. The second comment is referring to the parameter type in the following line of code. Therefore, in the second code comment, "Book" is what is "expected" (by the h type), and "Buyable" is what is "provided" by the f parameter type.