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

"this" in inner class

5 replies
Chao Sun
Joined: 2010-09-26,
User offline. Last seen 42 years 45 weeks ago.

Hi all,

I have some fairly simple code like the following:

class A {
class B(foo: Int, bar: Int) {
this(foo: Int) = this(foo, this.foo)
}
}

However, scalac will complain "value foo is not a member of A". So, is
there anyway to
get reference for class B?

Chris

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: "this" in inner class

On Sun, Sep 26, 2010 at 10:55:23AM -0700, Chao Sun wrote:
> class A {
> class B(foo: Int, bar: Int) {
> this(foo: Int) = this(foo, this.foo)
> }
> }
>
> However, scalac will complain "value foo is not a member of A". So, is
> there anyway to get reference for class B?

Your question is very unclear. Neither A nor B has a member foo.
However if B did have a foo, you couldn't reference it from an auxiliary
constructor (assuming "this" is intended to be "def this") since the
instance isn't constructed yet. But in the example given, foo has been
passed as a parameter, so why would you want to refer to this.foo? Even
if it were a member and you could reference it, you already have it.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: "this" in inner class

Oh, and I accidentally left off the syntax you're probably seeking, even
if it won't do you much good in the example as given:

B.this.foo

Chao Sun
Joined: 2010-09-26,
User offline. Last seen 42 years 45 weeks ago.
Re: "this" in inner class

Thanks Paul, sorry that my code is unclear. In my original code, I
want to define a data structure for a tree node, and I want to pass
the information of path from root node (which is a list of tree
nodes) to the node class. In the constructor I need to put the current
node into its path. (I know I could do it outside though...)

Also, for the "B.this.foo", I got an error "B is not a enclosing
class". Do you know why is that?

Thanks,
Chris

On Sun, Sep 26, 2010 at 11:15 AM, Paul Phillips wrote:
> Oh, and I accidentally left off the syntax you're probably seeking, even
> if it won't do you much good in the example as given:
>
>  B.this.foo
>
> --
> Paul Phillips      | Those who can make you believe absurdities
> Everyman           | can make you commit atrocities.
> Empiricist         |     -- Voltaire
> i'll ship a pulp   |----------* http://www.improving.org/paulp/ *----------
>

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: "this" in inner class
On Sun, Sep 26, 2010 at 2:49 PM, Chao Sun <sunchao [dot] chris [at] gmail [dot] com> wrote:

Also, for the "B.this.foo", I got an error "B is not a enclosing
class". Do you know why is that?

It's because you don't have a B yet, because you've not made it into the real constructor yet!

Paul's syntax is fine, as you can see here:

class A(val foo: Int) {
  class B(val foo: Int) {
    def c = A.this.foo + B.this.foo
  }
  val b = new B(foo*2)
}
scala> (new A(3)).b.c
res20: Int = 9
 
You need to think much more carefully about what you are doing.  Note:

class B(i: Int) {
  def this(s: String) = this(s.length)  // There is _no B to work with here_, cannot refer to i
}

You must get into the class through its main constructor before it exists.

Maybe you want a method that generates a new class of the same type?   Then you need to call new, like so, and should not try to make it look like a constructor:

class B(i: Int) {
  def another(s: String) = new B(i + s.length)  // This is a regular method, so we can use i
}

  --Rex

Jefferson Andrade
Joined: 2010-04-09,
User offline. Last seen 42 years 45 weeks ago.
Re: "this" in inner class
Can you send your code? I think it would be much simpler to answer the question if the code could be analysed.

Jefferson Andrade.


On Sun, Sep 26, 2010 at 3:49 PM, Chao Sun <sunchao [dot] chris [at] gmail [dot] com> wrote:
Thanks Paul, sorry that my code is unclear. In my original code, I
want to define a data structure for a tree node, and I want to pass
the information of path from root node  (which is a list of tree
nodes) to the node class. In the constructor I need to put the current
node into its path. (I know I could do it outside though...)

Also, for the "B.this.foo", I got an error "B is not a enclosing
class". Do you know why is that?

Thanks,
Chris

On Sun, Sep 26, 2010 at 11:15 AM, Paul Phillips <paulp [at] improving [dot] org> wrote:
> Oh, and I accidentally left off the syntax you're probably seeking, even
> if it won't do you much good in the example as given:
>
>  B.this.foo
>
> --
> Paul Phillips      | Those who can make you believe absurdities
> Everyman           | can make you commit atrocities.
> Empiricist         |     -- Voltaire
> i'll ship a pulp   |----------* http://www.improving.org/paulp/ *----------
>



--
"You question the worthiness of my Code? I should kill you where you stand!"

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