Fahad/CodeSamples/Main.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 10 Jun 2015 14:51:35 +0100
changeset 79 ca8f9645db69
parent 44 a751aa1ee4f7
permissions -rw-r--r--
added frisch / cardelli paper
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     1
package Main
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     2
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     3
abstract class Buffer{
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     4
  type T
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     5
  val element: T
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     6
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     7
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     8
abstract class SeqBuffer extends Buffer{
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     9
  type U
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    10
  type T <: Seq[U]
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    11
  def length = element.length
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    12
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    13
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    14
abstract class IntSeqBuffer extends SeqBuffer{
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    15
  type U = Int
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    16
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    17
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    18
object AbstractTypeTest1 extends App{
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    19
  def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer =
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    20
    new IntSeqBuffer{
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    21
	  type T = List[U]
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    22
	  val element = List(elem1, elem2)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    23
  }
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    24
  val buf = newIntSeqBuf(7, 8)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    25
  println("length = " + buf.length)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    26
  println("content = " + buf.element)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    27
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    28
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    29
abstract class Buffer2[+T] {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    30
  val element: T
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    31
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    32
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    33
abstract class SeqBuffer2[U, +T <: Seq[U]] extends Buffer2[T] {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    34
  def length = element.length
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    35
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    36
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    37
object AbstractTypeTest2 extends App {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    38
  def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer2[Int, Seq[Int]] =
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    39
    new SeqBuffer2[Int, List[Int]] {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    40
      val element = List(e1, e2)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    41
    }
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    42
  val buf = newIntSeqBuf(7, 8)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    43
  println("length = " + buf.length)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    44
  println("content = " + buf.element)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    45
}