So here is a puzzle. What will be written by
The answer is false.
Why? Because the line with points.contains(Point(3, _))
instead of performing pattern matching, checks whether points
contain a function:
The strangest thing for me is that the compilation of
this code does not generate any warnings.
From Scala compiler point of view the above code is
perfectly valid and this in turn is the result of
List type being covariant.
Or in the other words because we can assign List[Point] to
List[Any], contains must accept
arguments of any type:
The declaration of contains method in List[A] looks like this:
We may snoop the actual types assigned to the generic parameters
using a helper method:
So during the compilation A1 becomes Object and everything
Let’s finish by writing a code that actually does what
the programmer intended: