-
-
Save Yuichiroh/141317a013b72f913ae4 to your computer and use it in GitHub Desktop.
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 Student(val sex:String) | |
class Boy(val fightingInstinct:Int = 100) extends Student("male") | |
class Girl(val motherhood:Int = 100) extends Student("female") | |
class School[T <: Student](val students: Seq[T]){ | |
def studentsAsArray = students.toArray | |
// Error: No ClassTag available for T | |
// def studentsAsArray = students.toArray | |
// ^ | |
// Error: not enough arguments for method toArray: (implicit evidence$1: scala.reflect.ClassTag[T])Array[T]. | |
// Unspecified value parameter evidence$1. | |
// def studentsAsArray = students.toArray | |
// ^ | |
def studentsAsList = students.toList | |
// OK | |
} | |
object SchoolTest { | |
val boysSchool = new School(Seq(new Boy, new Boy, new Boy)) | |
boysSchool.students.map(_.fightingInstinct).sum | |
boysSchool.studentsAsArray // cannot compile | |
boysSchool.studentsAsList // OK | |
boysSchool.students.toArray // OK | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment