내 함수에서 반환 형식을 사용하여 개체 또는 문자열을 가져 오는 중 하나를 사용하려고합니다. 개체 인 경우이 개체에서 메서드를 호출하기 시작합니다. 문자열 인 경우에는 다른 곳에서 다른 함수를 호출하고 싶습니다. 나는 반환되는 것이 반환되는 객체가 아니기 때문에 끊기고 있습니다.이 객체는 "left"유형이며 "Left"유형에서 해당 객체를 "Player"유형으로 다시 가져올 수 없습니다. 내가 좋아하는 것. 이것은 가변 큐를 확장하고있는 오브젝트에 있습니다. 여기 다시 참조하거나 개체로 다시
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)
}
}
내 함수 중 하나 "플레이어"개체를 반환하는 기능을 사용하려고 : 여기 내 ActionQueue 객체에있는 키를 기반으로지도에서 플레이어 오브젝트를 조회 내 기능입니다 또는 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.
}
}
나는 나를 solicitDecision 함수를 호출하고 반환 무엇을 얻을 수 있도록 않는 .fold 기능을 사용하여 시도했다, 그러나 나는 직접 플레이어 객체를 사용하고 싶습니다. 확실히 이것은 가능합니다. 누군가 도울 수 있습니까?
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)
''solicitDecision'의 리턴 타입은 무엇입니까? 'currentPlayer.fold (...)'에서 무엇을 얻을 것으로 기대하십니까? – danielnixon
내부에 객체가 없기 때문에 'Either'에서 객체를 가져올 수 없습니다. – ziggystar