0
import scala.collection.mutable.ArrayBuffer object nameList { val names = ArrayBuffer("placeholder") } class Robot { val r = scala.util.Random val letters = 'A' to 'Z' val name = { val initName = nameList.names(0) while(nameList.names.contains(initName)){ val initName = letters(r.nextInt(26)).toString + letters(r.nextInt(26)).toString + r.nextInt(10).toString + r.nextInt(10).toString + r.nextInt(10).toString println("while", initName) println("while", nameList.names) println("checker", nameList.names.contains(initName)) } println("outside", nameList.names) nameList.names += initName initName } } 

outputs

(while,LA079) (while,ArrayBuffer(placeholder)) (checker,false) (while,IO176) (while,ArrayBuffer(placeholder)) (checker,false) 

The while loop runs indefinitely, above is an output snippet. Why isn't the while loop exiting even though the condition is changed to false?

Big picture, I need to ensure that each Robot instance has a unique name--I'm open to alternatives to using a while loop.

Update: per Jason C, below code fixes reassignment problem:

var initName = nameList.names(0) while(nameList.names.contains(initName) == true){ initName = letters(r.nextInt(26)).toString + letters(r.nextInt(26)).toString + r.nextInt(10).toString + r.nextInt(10).toString + r.nextInt(10).toString 
2
  • You redefined initName -- one shadows the other. Commented Aug 29, 2016 at 1:47
  • initName is a val. It cannot, and does not, change. The initName inside the while loop is a different variable. Commented Aug 29, 2016 at 1:47

1 Answer 1

3

It's because in your loop:

val initName = ... while(nameList.names.contains(initName)){ val initName = ... ... } 

You redeclare val initName in the loop. So now you effectively have two different values. The one in the while condition is the outer scoped one. The one declared in the loop has no effect on it.

I don't actually know Scala but from What is the difference between a var and val definition in Scala? I'm guessing the solution is to change the outer one to var (so it's modifiable) and drop the val entirely from the inner one (so you're not redeclaring it).

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

1 Comment

Thank you, your solution fixed it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.