- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
isTrait and isModuleClass at the same time?
Mon, 2010-08-30, 19:13
Hi,
While compiling scala.collection.BitSetLike I've got a symbol which
returns true for both isTrait and isModuleClass. I've checked how it
looks after compiling into bytecode and I'm rather confused:
javap 'BitSetLike$class'
Compiled from "BitSetLike.scala"
public abstract class scala.collection.BitSetLike$class extends
java.lang.Object{
public static int size(scala.collection.BitSetLike);
public static scala.collection.Iterator
iterator(scala.collection.BitSetLike);
public static void foreach(scala.collection.BitSetLike, scala.Function1);
public static scala.collection.BitSetLike
$bar(scala.collection.BitSetLike, scala.collection.BitSet);
public static scala.collection.BitSetLike
$amp(scala.collection.BitSetLike, scala.collection.BitSet);
public static scala.collection.BitSetLike
$amp$tilde(scala.collection.BitSetLike, scala.collection.BitSet);
public static scala.collection.BitSetLike
$up(scala.collection.BitSetLike, scala.collection.BitSet);
public static boolean contains(scala.collection.BitSetLike, int);
public static boolean subsetOf(scala.collection.BitSetLike,
scala.collection.BitSet);
public static scala.collection.mutable.StringBuilder
addString(scala.collection.BitSetLike,
scala.collection.mutable.StringBuilder, java.lang.String,
java.lang.String, java.lang.String);
public static java.lang.String stringPrefix(scala.collection.BitSetLike);
public static void $init$(scala.collection.BitSetLike);
}
so this class contains only static methods which is consistent with
isModuleClass but at the same time it's an abstract class which
suggests that some other class might be inheriting from this one.
Can someone shed a light when something like isTrait && isModuleClass
occurs and how it corresponds to Scala code?
Sun, 2010-09-05, 07:07
#2
Re: {mailcleaner} isTrait and isModuleClass at the same time?
On Tue, Aug 31, 2010 at 4:07 AM, iulian dragos wrote:
> You're probably running your phase after 'mixin'.
I'm running my code after cleanup which means, yes, after mixin.
> A trait is translated into an interface and an implementation class. The
> implementation class carries all concrete methods of the trait, encoded as
> static methods that take an explicit 'this' parameter. For instance:
> trait A {
> def foo
> def bar(x: Int) = x + 1
> }
> results (after mixin) in:
> // Java interface
> trait A {
> def foo
> def bar(x: Int)
> }
>
> // impl class
> abstract class A$class {
> def bar($this: A, x: Int) = x + 1
> }
> You are probably looking at A$class, which comes from the original trait A.
> This encoding allows multiple trait inheritance. Suppose class C mixes in A.
> C is translated to implement interface A and delegate all concrete methods
> to A$class:
> class C implements A {
> def bar(x: Int) = A$class.bar(this, x)
> // ...
> }
Yep, makes sense. I've got more educated myself about these Symbol.is*
methods so I understand better the relation between trees representing
trait and symbols representing them.
Thanks for the answer.
A trait is translated into an interface and an implementation class. The implementation class carries all concrete methods of the trait, encoded as static methods that take an explicit 'this' parameter. For instance:
trait A { def foo def bar(x: Int) = x + 1}
results (after mixin) in:
// Java interfacetrait A { def foo def bar(x: Int)}
// impl classabstract class A$class { <static> def bar($this: A, x: Int) = x + 1}
You are probably looking at A$class, which comes from the original trait A.
This encoding allows multiple trait inheritance. Suppose class C mixes in A. C is translated to implement interface A and delegate all concrete methods to A$class:
class C implements A { def bar(x: Int) = A$class.bar(this, x) // ...}
iulian
On Mon, Aug 30, 2010 at 8:14 PM, Grzegorz Kossakowski <grek@google.com> wrote:
--
« Je déteste la montagne, ça cache le paysage »
Alphonse Allais