为什么这个片段没有打印出来:“你成功地实现了这个功能”
细节:
为什么 val actual 的类型似乎是 List[Either[List[Int], Int]] 类型,而我什至明确对编译器说实际的类型应该是 List[Int] 类型?
// Flatten a nested list structure
def flatten[T](list: List[Either[List[T], T]]): List[T] = list flatMap {
// TODO: Implement
case list: List[T] => list
case element: T => List(element)
}
implicit def ElementToEitherLeft[T](obj: T) = Left(obj)
implicit def ElementToEitherRight[T](obj: T) = Right(obj)
val list: List[Either[List[Int], Int]] = List(List(1, 1), 2, List(3, 5))
val actual: List[Int] = flatten[Int](list)
val expected = List(1, 1, 2, 3, 5)
if (actual == expected) print("You successfully implemented the function")
else print("Unfortunatly, that's not quite rigth yet")