Created
July 12, 2011 22:42
-
-
Save anrizal06/1079163 to your computer and use it in GitHub Desktop.
Creating List with elements are of type A and B consecutively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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