I've searched around for a long time now and haven't come up with any official reason why scala doesn't support multiple inheritance. I know traits solve a lot of things, but the developers of C# for example give an explicit reason why not to implement it (link here).
Any pointers?
-
1This sounds like you're asking for off-site resources.raptortech97– raptortech972015年01月02日 18:14:57 +00:00Commented Jan 2, 2015 at 18:14
-
1Related: programmers.stackexchange.com/questions/218458/…Telastyn– Telastyn2015年01月02日 18:27:48 +00:00Commented Jan 2, 2015 at 18:27
-
3The reasons are almost certainly the same reasons that Java and C# don't support it.Robert Harvey– Robert Harvey2015年01月02日 20:54:04 +00:00Commented Jan 2, 2015 at 20:54
2 Answers 2
One of Scala's primary design goals is to interoperate with Java as smoothly as possible. In order to achieve this, a Scala class must have a one-to-one correspondence with a JVM class. Because the JVM ddoesn't support multiple inheritance, it would therefore be impossible for Scala to do so without introducing massive hacks.
-
1Source for your statement "the JVM doesn't support multiple inheritance": the JVM Spec, section 4.1 "The
ClassFile
Structure" states that class descriptions contain a singlesuper_class
field that can either be empty or reference a superclass, whereas classes can reference any number of interfaces.amon– amon2015年01月03日 12:20:01 +00:00Commented Jan 3, 2015 at 12:20
Scala does support Multiple Inheritance: you can inherit from as many traits as you want.
So, the reason for why there is no official documentation for the reasons for not supporting MI is simply that there aren't any, because Scala does support MI. You say "traits solve a lot of things", and MI is one of them.
-
3Yes, any class or trait may inherit from multiple traits. But for some reason, you can't inherit from more than one class. Why and how are traits and classes different? Why this restriction on class inheritance? I do not believe this post answers the question.amon– amon2015年01月03日 09:29:30 +00:00Commented Jan 3, 2015 at 9:29
-
per my reading of the question, asker appears to be already aware of that: "I know traits solve a lot of things" (in the light of this, concern raised by @amon seems to make much sense)gnat– gnat2015年01月03日 10:40:39 +00:00Commented Jan 3, 2015 at 10:40
-
2@gnat: the OP says "I know traits solve a lot of things", but he doesn't elaborate on what those "lot os things" and what is missing from those "lot of things". His main point seems to be that there is no official documentation for the absence of MI, and the reason why there is no official documentation for the absence of MI is that there is no absence of MI.Jörg W Mittag– Jörg W Mittag2015年01月03日 12:54:29 +00:00Commented Jan 3, 2015 at 12:54