简体   繁体   中英

what is difference in creating instance with or without generic type in scala?

I have a class definition and two function definition

class Queue[T] (
  private val leading: List[T]
  private val trailing: List[T]
){}

def a[T](xs: T*) = new Queue[T](xs.toList, Nil)

def b[T](xs: T*) = new Queue(xs.toList, Nil)

From here, class is defined with generic type, and also two function does.

But here, the difference between two functions is when creating an instance.

Both work well and no error is created with following expression:

a(1,2,3,4)
b(1,2,3,4)

a[Int](1,2,3,4)
b[Int](1,2,3,4)

But, I couldn't figure out what the difference is.

There is none. The type parameter T of class Queue will be inferred to be the type parameter T of the method b .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM