0

This is my Scala function:

def combine(occur: Occurrences): List[List[Word]] = {
  if (occur.isEmpty) List(List()) 
  else {
    for {
      combintaion <- combinations(occur)
      words = getWords(combintaion)
      if !words.isEmpty
      word: Word <- words
      rest <- combine(subtract(occur, combintaion))
    } yield List(word) :: rest
  }
}

I expect it to return List[List[Word]], but compiler says, that for expression returns List[List[Object]]. Why so, and what should I do?

2
  • what does getWords return? Commented Feb 1, 2014 at 12:37
  • 1
    You should change yield List(word) :: rest to yield word :: rest Commented Feb 1, 2014 at 13:28

2 Answers 2

5

You should change

yield List(word) :: rest

to

yield word :: rest

since rest is type of List[Word] and word :: rest is still type of List[Word], hence for(..) yield word :: rest is List[List[Word]].

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

Comments

0

You are returning two type of values here.

Use

val result:List[List[Word]]=List()
return result;

Insted of

if (occur.isEmpty) List(List()) 

because its return type is List[List[Nothing]]

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.