This page is no longer maintained — Please continue to the home page at www.scala-lang.org

isModule, isModuleClass, etc.

3 replies
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

Can anyone clarify for me how everything relates here:

def isClass = false //to be overridden
final def isModule = isTerm && hasFlag(MODULE)
final def isModuleClass = isClass && hasFlag(MODULE)

final def isPackageClass = isClass && hasFlag(PACKAGE)
final def isPackageObject = isModule && name == nme.PACKAGEkw && owner.isPackageClass
final def isPackageObjectClass = isModuleClass && name.toTermName == nme.PACKAGEkw && owner.isPackageClass

I can see the relationship is in theory like this:

* Then object Foo has a `moduleClass' (invisible to the user, the backend calls it Foo$
* linkedClassOFClass goes from class Foo$ to class Foo, and back.

So I read this as: "object Foo" is a ModuleSymbol (a TermSymbol) and has
an invisible associated class "class Foo$", a ModuleClassSymbol (a
TypeSymbol) which is what actually holds all the members of object Foo.
So when will a symbol have "object Foo" as its owner?

The immediate motivation for this question is that I tried to use this
method:

final def isPredefModule = isModule && name == nme.Predef && owner.isScalaPackageClass // not printed as a prefix

And it returns false where I'm using it because although the last two
conditions are true, isModule is false because the symbol's owner is
Predef's module class. I assume this is also what's going on in this
excerpt from the email I sent on 6 Nov about the optimizer slowdown:

[log inliner] Reading class: object Predef isScalaModule?: false

In that case also, isModule is false for something named "object Predef"
so it must be the module class.

Sometimes these are checked together, as in these examples:

checkBool((module.isModule || module.isModuleClass),
"Expected module: " + module + " flags: " + Flags.flagsToString(module.flags));

final def isInterpreterWrapper =
(isModule || isModuleClass) &&

...but much more often only one or the other is checked.

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: isModule, isModuleClass, etc.

On Fri, Nov 27, 2009 at 5:37 PM, Paul Phillips wrote:
> Can anyone clarify for me how everything relates here:
>
>    def isClass  = false         //to be overridden
>    final def isModule = isTerm && hasFlag(MODULE)
>    final def isModuleClass = isClass && hasFlag(MODULE)
>
>    final def isPackageClass = isClass && hasFlag(PACKAGE)
>    final def isPackageObject = isModule && name == nme.PACKAGEkw && owner.isPackageClass
>    final def isPackageObjectClass = isModuleClass && name.toTermName == nme.PACKAGEkw && owner.isPackageClass
>
> I can see the relationship is in theory like this:
>
>     *  Then object Foo has a `moduleClass' (invisible to the user, the backend calls it Foo$
>     *  linkedClassOFClass goes from class Foo$ to class Foo, and back.
>
> So I read this as: "object Foo" is a ModuleSymbol (a TermSymbol) and has
> an invisible associated class "class Foo$", a ModuleClassSymbol (a
> TypeSymbol) which is what actually holds all the members of object Foo.

Yes, all correct.

> So when will a symbol have "object Foo" as its owner?
>
Never. The owner is always the module class.

Cheers

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: isModule, isModuleClass, etc.

On Fri, Nov 27, 2009 at 05:54:53PM +0100, martin odersky wrote:
> > So when will a symbol have "object Foo" as its owner?
> >
> Never. The owner is always the module class.

OK, that is the best answer from an understandability perspective: then
those places checking both are bugs (innocuous bugs, but still bugs) so
I will remedy them.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: isModule, isModuleClass, etc.

BTW I take that last bit back, I realize they're not necessarily
redundant.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland