6

I'm new to Scala, coming from Java, and I was just reading about traits. One thing that gets mentioned often is that traits don't (can't? won't?) have constructor parameters. I was curious to know if there was a reason for this.

Coming from a long ago maths/computer-science background I was was wondering if this was an inevitable consequence because of some language design decision, or if it was a conscious decision to avoid some inheritance/mix-in problem or another?

Was hoping someone might know because it feels like there might be something interesting behind the fact.

moncheery
  • 333
  • 4
  • 17
  • Considering you can pretty much emulate constructor parameters by overriding vals and doing the rest of the constructor body lazily, I doubt that there's a deep reason behind it. Of course, maybe there is. Maybe something to do with Java/Scala interop? – Cubic Apr 01 '15 at 14:00
  • Traits don't have constructors. That's pretty much the defining difference between classes and traits – Martijn Apr 01 '15 at 14:01
  • Related: http://stackoverflow.com/questions/2804041/constructor-in-an-interface – Michael Zajac Apr 01 '15 at 15:09

6 Answers6

11

The other answers describe the language; I suspect your question may really be "why is it designed in this way".

I believe it arises out of the awkwardnesses and verboseness that would arise when extending multiple traits, especially with overrides and with types, and various mix-in strategies.

The Cake Pattern often results in various traits providing missing bits to each other in a way that is totally invisible - by design - in the mixing class. And mixing can be bi-directional, using self-types. So the construction of a class from traits can be a very messy business for the compiler. Scala often trades simplicity of compiler design and implementation for simplicity of language use and code reduction, and this is certainly a good example.

So while there may be simple, hierarchical cases where having a constructor might be useful and sufficient, it would almost certainly have to be redundant of other mechanisms for more difficult, non-hierarchical scenarios.

Community
  • 1
  • 1
Ed Staub
  • 15,480
  • 3
  • 61
  • 91
  • I don't agree with the conclusion, but gave +1 because you tried to answer the question as it was (probably?) intended. – Madoc Apr 01 '15 at 14:59
  • @Madoc - It's a weak answer - I'm embarrassed by all the upvotes. Hopefully, someone like Daniel will weigh in with specifics. – Ed Staub Apr 01 '15 at 15:12
  • 1
    You are right about the intention behind my question though :) so thank you. – moncheery Apr 01 '15 at 16:13
5

Scala 3 will allow trait parameters. Here's a sample from the docs

trait Greeting(val name: String) {
  def msg = s"How are you, $name"
}

class C extends Greeting("Bob") {
  println(msg)
}
joel
  • 6,359
  • 2
  • 30
  • 55
3

The answer is: that's what Scala is right now.

But that might not be the case in the future: trait parameters can replace early initializers. (see Martin Odersky's recent Scala Days presentation page 34) Scala: Where It Came From & Where It is Going

lcn
  • 2,239
  • 25
  • 41
2

Traits don't have constructor parameters because traits cannot be constructed. Given any trait T it's not possible to instantiate any object of type exactly T. You can override trait defs with vals though, so

trait Foo {
  def bar: String
}
class Baz(override val bar: String) extends Foo

You can't construct them directly because new MyTrait {} is actually sugar for an anonymous class of new Object with MyTrait {}

Daenyth
  • 35,856
  • 13
  • 85
  • 124
  • 6
    Hmm. The same logic would apply to abstract classes, wouldn't it? I would say that traits *should* be allowed to have constructors, and I also don't understand why they are not allowed to. – Madoc Apr 01 '15 at 14:58
0

Trait is analog for Java Interface. The main difference is that trait can have default implementation for their methods.

So Java interfaces can't have constructor so do Scala traits

Lucky Libora
  • 140
  • 1
  • 3
0

Scala 3 allows traits with parameters, just like classes have parameters.

shvahabi
  • 1,218
  • 10
  • 7