Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala multiple with

Tags:

scala

traits

New to Scala. The language is quite concise.

Curious why implementing multiple traits requires multiple "with" statements.

For example:

class Foo extends Bar with A with B with C {}

vs.

class Foo extends Bar with A, B, C {}
like image 971
virtualeyes Avatar asked Nov 15 '11 16:11

virtualeyes


People also ask

Can you extend multiple traits in Scala?

We can extend from multiple traits, but only one abstract class. Abstract classes can have constructor parameters, whereas traits cannot.

What does <: mean in Scala?

It means an abstract type member is defined (inside some context, e.g. a trait or class), so that concrete implementations of that context must define that type.

Can you extend multiple classes in Scala?

While we cannot extend multiple abstract classes, we can extend multiple traits in Scala.

What is trait in Scala with example?

In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.


1 Answers

Consider this

class Foo[A, B]

new Foo[Bar with D with E, Bar with E] {}

vs

new Foo[Bar with D, E, Bar with E] {}

It just isn’t unambiguous in all cases.

like image 111
Debilski Avatar answered Sep 22 '22 06:09

Debilski