Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Members and Value Members of a Scala Class

Tags:

scala

In Scala, classes can have Type Members and Value Members, I am just wondering what is the difference between these two and when would you use one of these.

like image 447
user2456976 Avatar asked Sep 14 '25 11:09

user2456976


1 Answers

Value members (or rather term members) are class members that represent some value. These are: defs, vals, vars and inner objects.

Type members are members that represent a type. These are inner classes, traits and abstract types or type aliases (declared or defined with keyword type).

abstract class A {
  // examples of term members
  val someVal = 5
  var someVar = 0
  def someMethod(someParam: Int) = someParam * 2
  object someInnerObject

  // examples of type members
  type SomeTypeAlias = List[String]
  type SomeAbstractType
  trait SomeInnerTrait
  class SomeInnerClass
}

I don't know if there is anything more significant to say about this classification. I hope someone can give some more general explanation if there is one.

like image 133
ghik Avatar answered Sep 16 '25 10:09

ghik