Concrete Traits called Abstract

72 views
Skip to first unread message

archsky

unread,
Dec 23, 2015, 12:21:31 AM12/23/15
to scala-user
Correct if wrong:

1) Traits, by definition, cannot be instantiated into objects by themselves.
2) Traits can have concrete as well as abstract members.

------------------

Now, this piece of code:

scala> trait test { val member = 44 }
defined trait test

scala> val testobject = new test
<console>:11: error: trait test is abstract; cannot be instantiated
       val testobject = new test
                        ^

REPL says test is abstract, while it is not.
Any ideas about this?

Also, it feels a bit not right if a trait is being called abstract or concrete per se. It's the members that are abstract or concrete.

(Q) AAMOF, what should a trait having both abstract and concrete members, be called?

Not that the issue is about some bug about language's core functionality; I feel a tiny bit of improvement implied here in terms of proper naming.

Luis Ángel Vicente Sánchez

unread,
Dec 23, 2015, 12:26:22 AM12/23/15
to archsky, scala-user

if you tried instantiating it like this:

val testObject = new test { }

it would work. So... it might be abstract because it has no constructor implemented? I'm just guessing so... I'm probably wrong

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oliver Ruebenacker

unread,
Dec 23, 2015, 9:58:28 AM12/23/15
to Luis Ángel Vicente Sánchez, archsky, scala-user

     Hello,

  The notation "new A {}" does not instantiate A, but rather instantiates an anonymous subclass of A. That's why it may work even if A is abstract.

  It is common in computer science to call types that are not meant to be instantiated abstract (whether or not such a design choice is enforced by the language), so it seems an appropriate term for traits, which can not be instantiated.

  And yes, traits don't have constructors because in JVM world, we don't do full multiple inheritance to keep life simpler.

     Best, Oliver
--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Sonnenschein

unread,
Dec 26, 2015, 3:35:26 AM12/26/15
to scala-user
It is right: You can mix in traits but no classes. In case of mixin composition it does not matter whether the trait has abstract methods or not.
Btw., in Java they also use marker interfaces.
Reply all
Reply to author
Forward
0 new messages