2

I have a special class Model that needs to have its methods called in a very specific order.

I tried doing something like this:

val model = new Model

new MyWrappingClass {
 val first = model.firstMethod()
 val second = model.secondMethod()
 val third = model.thirdMethod()

}

The methods should be called in the order listed, however I am seeing an apparently random order.

Is there any way to get the variable initialization methods to be called in a particular order?

5
  • How do you tell it's called in random order? Commented Aug 28, 2015 at 6:34
  • @thirstycrow It seems that way due to an error I am having. But even if it turns out to be something else I am interested in knowing. This should be a Scala spec but I could not find it. Commented Aug 28, 2015 at 6:35
  • You sure, this is as close to your actual code as you think? When I implement it with println in the model methods, the order seems to be correct. Your actual code does not declare these vals as lazy val by any chance? Commented Aug 28, 2015 at 6:36
  • @SaschaKolberg No lazy vals, this is as close as it gets. It could well be that most of the time they execute in order, but how about all of the time? Commented Aug 28, 2015 at 6:37
  • I think, there is something crucial missing. Unless your methods are not doing something concurrently or you have a more complex inheritance hierarchy this looks pretty straight forward sequential to me. Commented Aug 28, 2015 at 6:41

2 Answers 2

1

I doubt your methods are called in the wrong order. But to be sure, you can try something like this:

val (first, second, third) = (
   model.firstMethod(),
   model.secondMethod(),
   model.thirdMethod()
)

You likely have some other problem with your code.

Sign up to request clarification or add additional context in comments.

Comments

0

I can run 100 million loops where it never gets the order wrong, as follows:

class Model {
   var done = Array(false,false,false);
   def firstMethod():Boolean =  { done(0) = true; done(1) || done(2) };
   def secondMethod():Boolean = { done(1) = true; !done(0) || done(2) };
   def thirdMethod():Boolean  = { done(2) = true; !done(0) || !done(1) };
};

Notice that these methods return a True if done out of order and false when called in order.

Here's your class:

class MyWrappingClass {
 val model = new Model;
 val first = model.firstMethod()
 val second = model.secondMethod()
 val third = model.thirdMethod()
};

Our function to check for bad behavior on each trial:

def isNaughty(w: MyWrappingClass):Boolean = { w.first || w.second || w.third };

A short program to test:

var i = 0
var b = false;

while( (i<100000000) && !b ){
       b = isNaughty(new MyWrappingClass);
       i += 1;
}

if (b){
    println("out-of-order behavior occurred");
    println(i);
} else {
    println("looks good");
}

Scala 2.11.7 on OpenJDK8 / Ubuntu 15.04

Of course this doesn't prove it impossible to have wrong order, only that correct behavior seems highly repeatable in a fairly simple case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.