Skip to content

Instantly share code, notes, and snippets.

@anrizal06
Created July 12, 2011 22:42
Show Gist options
  • Save anrizal06/1079163 to your computer and use it in GitHub Desktop.
Save anrizal06/1079163 to your computer and use it in GitHub Desktop.
Creating List with elements are of type A and B consecutively
class MyA[A, B](val head: A, val tail: Option[MyA[B, A]]) {
override def toString() = head + tail.map( "," + _.toString).getOrElse("")
def ::( x: B) = new MyA(x, Some(this))
}
object MyA {
def apply[A,B](value: A) : MyA[A, B] = new MyA(value, None)
}
// examples:
5 :: "help" :: 6 :: "kjl" :: 9 :: MyA("d") // 5,help,6,kjl,9,d
6:: "six" :: 7 :: "seven" :: 8 :: "eight" :: MyA(9) // 6,six,7,seven,8,eight,9
6:: "six" :: 7 :: "seven" :: "eight" :: MyA(9) // does not compile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment