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

a question about classof[Foo]

3 replies
Caesar You
Joined: 2009-09-07,
User offline. Last seen 42 years 45 weeks ago.

if i define an object
 
    object Foo
 
and i use classof[Foo]
 
and there is a compile error: not found: type
Foo
 
why? isnt the compile create a class Foo for the
standalone object?

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: a question about classof[Foo]

Are you using the REPL for this?

On Friday, September 11, 2009, Caesar You wrote:
>
>
>
>
>
> if i define an object
>
>     object Foo
>
> and i use classof[Foo]
>
> and there is a compile error: not found: type
> Foo
>
> why? isnt the compile create a class Foo for the
> standalone object?
>

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: a question about classof[Foo]


2009/9/11 Caesar You <Caesar [dot] Jr [at] hotmail [dot] com>
if i define an object       object Foo   and i use classof[Foo]


I'd suggest:

object Foo

val v = Foo.getClass
 
  and there is a compile error: not found: type Foo   why? isnt the compile create a class Foo for the standalone object?



--
Viktor Klang

Blog: klangism.blogspot.com
Twttr: viktorklang

Lift Committer - liftweb.com
AKKA Committer - akkasource.org
Cassidy - github.com/viktorklang/Cassidy.git
SoftPub founder: http://groups.google.com/group/softpub
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: a question about classof[Foo]
Also note that if you need to define a method that takes the an object of your singleton instance type, you do so with:

def useFoo(foo : Foo.type)


The issue here is classOf expects a type in the [], and there is no way (AFAIK) to specify the type of Foo such that classOf can satisfactorily figure out the class is Foo$ (i.e. classOf[Foo.type] does not work).  So stick with Victor's suggetion of Foo.getClass if you need the class object for some reason.

- Josh

2009/9/11 Caesar You <Caesar [dot] Jr [at] hotmail [dot] com>
if i define an object       object Foo   and i use classof[Foo]   and there is a compile error: not found: type Foo   why? isnt the compile create a class Foo for the standalone object?

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