Skip to content

Expand the Function1 example in the variances page #1748

@justinhj

Description

@justinhj

https://github.com/scala/docs.scala-lang/blob/67f117529298432e799cd88982240bb13a38301a/_tour/variances.md#other-examples

At the end of the page on variances the reader has to do a lot of work to figure out the "what does the animal eat example". I think it would be great if it included working code like the previous examples. However, it's a bit confusing in terms of what the "what does an animal eat" function should return. Should it return a type or an instance of animal?

One way to make it more concrete and clear would be to write a function that takes an Animal and a List[Animal] and filters out the ones that cannot be eaten. This way you could show example code that takes a Cat and a list of different animal instances, some of them being mice, and returns a List[Animal] that is clearly containing SmallAnimal.

This way we have shown contravariance and covariance in Function1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions