File: patmat-type-check.scala

package info (click to toggle)
scala 2.11.8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 61,060 kB
  • ctags: 3,923
  • sloc: java: 13,251; xml: 3,214; sh: 1,553; python: 756; makefile: 40; awk: 36; ansic: 6
file content (31 lines) | stat: -rw-r--r-- 1,074 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
object Test
{
  def s1 = "bob".toList  match { case Seq('b', 'o', 'b') => true }  // list ok

  // not final, allowed
  class Bop
  def s2(x: Bop) = x match { case Seq('b', 'o', 'b') => true }

  // covariance, allowed
  final class Bop4[+T]
  def s3[T](x: Bop4[T]) = x match { case Seq('b', 'o', 'b') => true }

  // contravariance, allowed
  final class Bop5[T, U, -V]
  def s4[T1, T2](x: Bop5[_, T1, T2]) = x match { case Seq('b', 'o', 'b') => true }

  // free type parameter, allowed
  final class Bop3[T]
  def f4[T](x: Bop3[T]) = x match { case Seq('b', 'o', 'b') => true }

  // String and Array are final/invariant, disallowed
  def f1 = "bob".reverse match { case Seq('b', 'o', 'b') => true } // fail
  def f2 = "bob".toArray match { case Seq('b', 'o', 'b') => true } // fail

  // final, no type parameters, should be disallowed
  final class Bop2
  def f3(x: Bop2) = x match { case Seq('b', 'o', 'b') => true } // fail

  // final, invariant type parameter, should be disallowed
  def f4[T](x: Bop3[Char]) = x match { case Seq('b', 'o', 'b') => true } // fail
}