aboutsummaryrefslogblamecommitdiff
path: root/tests/pos/inferred.scala
blob: 88038147c97087778303ad67b409f886fadde698 (plain) (tree)
1
2
3
4
5
6
7
8
9
                
  

                      
                   
  
                                                          
  
                                                                                     


  
                                  
                    
                            

                
 
                                                   



                     
 
 







                         
                                       
 
             
 
                   
 


                                     
 
                                   


                                         
                                                                
  

                       
                              
  
                              
  
 
class LIST[+T] {
  
  def isEmpty: Boolean
  def head: T
  def tail: LIST[T]
  
  def prepend [U >: T] (x: U): LIST[U] = new CONS(x, this)
  
  def map[U](f: T => U): LIST[U] = if (isEmpty) NIL else tail.map(f).prepend(f(head))
  
}

object NIL extends LIST[Nothing] {
  def isEmpty = true
  def head = throw new Error
  def tail = ???
}

class CONS[T](hd: T, tl: LIST[T]) extends LIST[T] {
  def isEmpty = false
  def head = hd
  def tail = tl
}


object Inferred {

  def foo[T](x: T): T = x

  val x = foo(1)

  val y = foo("abc")

  def bar[U](xs: LIST[U]): LIST[U] = xs

  val n = NIL

  val nn = bar(NIL)

  val ints: LIST[Int] = NIL prepend 1
  
  val ints1 = NIL prepend 1 prepend 2

  val a = if (1 == 0) NIL else ints
  
  val n2 = scala.collection.immutable.Nil
  
  val ss2: scala.collection.immutable.List[String] = "abc" :: n2
  
  val ss3 = "abc" :: n2
  
  def cl = ((x: Int) => x + 1)
  
  val ints2 = ints map (_ + 1)
  
}