Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add some action in constructor?

Naive question I believe, but all I find is just calling other constructors from constructors. I need to call a method. My class (beginning):

class ScopedIterator[T](val iter : Iterator[T])
{
  private var had_next : Boolean;
  private var value : T;

  moveNext();
  
  ...

so I would like to have a constructor with single argument, and in such constructor call a method moveNext. That's all.

When I compile the code I get error:

error: abstract member may not have private modifier

private var had_next : Boolean;

and the same for value.

I changed it to:

class ScopedIterator[T]
{
  private var had_next : Boolean;
  private var value : T;
  private var iter : Iterator[T];

  def this(it : Iterator[T]) =
  {
    iter = it;
    moveNext();
  }

  ...

But now I get error on "iter=it":

error: 'this' expected but identifier found.

iter = it;

How to write such constructor in Scala?

like image 227
greenoldman Avatar asked Oct 12 '11 11:10

greenoldman


People also ask

Can default constructor have parameters?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

What does a constructor do?

A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.

Are constructors methods?

A Constructor is a block of code that initializes a newly created object. A Method is a collection of statements which returns a value upon its execution. A Constructor can be used to initialize an object. A Method consists of Java code to be executed.


2 Answers

The first problem is that your definitions of had_next and value are abstract: these members have no right-hand side.

Try instead:

class ScopedIterator[T](val iter : Iterator[T]) {
  private var had_next : Boolean = _
  private var value : T = _
  ...
}

Here, _ means "default uninitialized value". For example, the following works for me in the console:

class ScopedIterator[T](val iter : Iterator[T]) {
  private var had_next : Boolean = _
  private var value : T = _

  init()

  def init() : Unit = { println("init !") }
}

scala> new ScopedIterator(List(1,2,3).toIterator)
init !
resN: ScopedIterator[Int] = ...

The second problem ("'this' expected...") comes because in Scala, auxiliary constructors must always call another constructor as their first statement. So your constructor could start with this(), for instance. For more details, see Section 6.7 in Programming in Scala.

like image 122
Philippe Avatar answered Nov 09 '22 16:11

Philippe


The default constructor is the one you define when you declare your class

Ex:

class greeting(name:String) { ... }

You can also define the default constructor to take no parameters like in your code

class greeting { ... }

Then you can add extra constructors. All constructors you add to the class need to call another constructor as the first statement of the constructor. If you omit that you get the "this expected but identifier found".

Let's see an example:

class classconstructor {
    var iter:Int = 0    
    def this(it:Int) = {      
      this()
      iter = it;
      moveNext();
    }   
    def moveNext() = {
      println(iter)   
    }
}

object App
{
    def main(args:Array[String])
    {
        val x = new classconstructor()
        val y = new classconstructor(200)       
    }
}

In the code above new classconstructor() does nothing because the empty constructor doesn't have body. and new classconstructor(200) executes the empty constructor + the new one where you can add extra code such as a call to moveNext() method. In this case this one prints 200 to the console.

like image 40
Carlos Quintanilla Avatar answered Nov 09 '22 17:11

Carlos Quintanilla