Skip to content

Instantly share code, notes, and snippets.

@Yuichiroh
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yuichiroh/141317a013b72f913ae4 to your computer and use it in GitHub Desktop.
Save Yuichiroh/141317a013b72f913ae4 to your computer and use it in GitHub Desktop.
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