Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For "trait Queue[T]", is `Queue` a type?

Defined a trait in scala:

trait Queue[T]

Is Queue a type? Or something else, e.g. a type constructor?

From http://artima.com/pins1ed/type-parameterization.html#19.3 of book "programming in scala", it says:

Queue, as defined in Listing 19.4, is a trait, but not a type.

But someone thinks it is a type, so I totally confused.

Is it a type or not? If not, what it is exactly?

like image 722
Freewind Avatar asked Mar 06 '26 14:03

Freewind


1 Answers

The following sentence answers your question:

Queue, as defined in Listing 19.4, is a trait, but not a type. Queue is not a type because it takes a type parameter.

We call Queue a generic type. You cannot use it alone, otherwise the compiler will complain

trait Queue takes type parameters

Try this:

type Q1 = Queue[Int]
//type Qwrong = Queue
like image 151
Arie Xiao Avatar answered Mar 09 '26 07:03

Arie Xiao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!