stella
stella

Reputation: 2596

Can we pass types as parameters to methods?

In scala we can have type members just declaraing type T. Can we pass types as method parameters? I tried this:

def test(type T)
def test(T: type)

but none of them compiled. So, is it possible?

Upvotes: 3

Views: 111

Answers (1)

user3248346
user3248346

Reputation:

No, it is not possible to pass types as values to a function in standard Scala.

Also, there are a couple of things wrong with your proposed code:

  • Firstly you cannot define a function parameter with the type keyword. This is used for declaring type synonyms not the type of a function parameter.

  • You cannot even pass a type as a value to a function in Scala. Here you are mixing at one level types in a program and on the other, what are values in your program. Scala functions accept values as arguments to a function (if that function is defined to accept arguments) not types.

Upvotes: 3

Related Questions