Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instantiate mix traits is compiling while single trait is not

Tags:

scala

I have two traits

trait Person
trait Food

why this is compiling :

val pf = new Person with Food //OK

and this is not

val p = new Person //result  error: trait Person is abstract; cannot be instantiated
like image 560
igx Avatar asked Nov 11 '22 14:11

igx


1 Answers

Traits are abstract (not instantiable) by definition. Even if they're fully implemented they may not be instantiated. As senia states in a comment, you can get an anonymous class from a fully implemented trait like this:

trait T1
val t1 = new T1 {}
like image 154
Randall Schulz Avatar answered Nov 14 '22 21:11

Randall Schulz