I'm trying to use an Either return type from my function to either get back an object or a string. In the case it is an object, I'd like to start calling methods from this object. In the case it is a string, I'd like to call some other functions elsewhere. I keep getting hung up because the thing being returned is not the object I'm returning, it is of type "left" and I can't seem to get that object out of the "Left" type back into the "Player" type I'd like. This is contained in an object that is extending the mutable queue. Here is my function which looks up the Player object in a Map based off the key which is in my ActionQueue object:
def popCurrentAction : Either[Player, String] = {
val currentAction = this.dequeue
this.enqueue(currentAction)
if (playerMap.get(currentAction) != None) {
Left((playerMap.get(currentAction).get))
}
else {
Right(currentAction)
}
}
Here is my function which is trying to use the function which returns Either a "Player" object or a String.
def doMove = {
var currentAction = ActionQueue.popCurrentAction
if (currentAction.isLeft) {
var currentPlayer = currentAction.left
var playerDecision = currentPlayer.solicitDecision() // This doesn't work
println(playerDecision)
}
else {
// Do the stuff if what's returned is a string.
}
}
I have tried using the .fold function, which does allow me to call the solicitDecision function and get what it returns, but I would like to use the Player object directly. Surely this is possible. Can someone help?
var currentPlayer = currentAction
var playerDecision = currentPlayer.fold(_.solicitDecision(), _.toString())
// This is close but doesn't give me the object I'm trying to get!
println(playerDecision)