1

I would like to access inner class type in the outer constructor, like :

// does not compile, MyInner now know from outer constructor
class MyOuter(private val mySet: Set[MyInner]) {
  class MyInner(index: Int)
}

The Inner class must be non static (outside an object) as it may access some fields of the outer class instance.

The code below compiles, but the field is mutable:

class MyOuter() {
  class MyInner(index: Int)

  private var mySet: Set[MyInner] = _
  def this(_mySet: Set[MyInner]) {
    this()
    mySet = _mySet
  }

This seems to be scala-specific as the below Java code is legal:

import java.util.Set;

public class Outer {

    private final Set<Inner> mySet;

    public class Inner {
        private final int index;

        public Inner(int _index) {
            index = _index;
        }
    }

    public Outer(Set<Inner> _mySet) {
        this.mySet = _mySet;
    }

}

Thanks for your help

ogen
  • 802
  • 2
  • 7
  • 23
  • 1
    It doesn't make sense for a constructor to take an argument of a type that is defined in terms of the thing you are trying to construct. – emilianogc Jan 30 '17 at 18:55
  • @emilianogc which pattern do you suggest then ? – ogen Jan 30 '17 at 19:49
  • Not sure what you're trying to achieve. You can't do instantiate new MyInner from outside to pass to the CTOR of MyOuter. In Java inner class means something else. – Maxim Jan 30 '17 at 21:31
  • @Maxim I would like to creaate a mapping between a map and an array of array. The inner class contains the two indices. For the purpose of this post, I have reduced the problem to a set and an array – ogen Jan 30 '17 at 21:43
  • @Maxim I would like to reproduice the builder pattern : http://stackoverflow.com/questions/5007355/builder-pattern-in-effective-java As the inner class is not instanciable outside of the outer class unlike java, how should I do in scala ? – ogen Jan 30 '17 at 21:49
  • @ogen in Scala if it's a simple builder just use default values. If you still want it why not making the builder not an inner class? – Maxim Jan 31 '17 at 22:05
  • 1
    @Maxim It is not a simple builder, I have made a separate class to handle this with package-private modifiers – ogen Feb 02 '17 at 10:10

1 Answers1

1

This phrase: "The Inner class must be non static (outside an object) as it may access some fields of the outer class instance." explains why what you want is impossible: you are trying to create an instance of a class, that accesses fields of an instance of another class, that does not yet exist.

And no, it would not work in java either. Note the static qualifier in the answer to the question you referenced in your comment. It has the same effect as moving your inner class to a companion object - static inner classes in java can only access static members of the outer class.

This would, I think, be the right solution for your case - make a companion object, and move the inner class there. No, you do not need to access members of the instance, that has not been constructed yet :)

Dima
  • 39,570
  • 6
  • 44
  • 70