user2456976
user2456976

Reputation: 1308

Type Members and Value Members of a Scala Class

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.

Upvotes: 4

Views: 1680

Answers (1)

ghik
ghik

Reputation: 10764

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.

Upvotes: 5

Related Questions