I keep getting "type mismatch" when trying to use a Seq[Seq[String]] in Scala. Why?
-
Here's my code. class Board (rows: String*) { var cells: Seq[Seq[String]] = rows.map(_.split("")) override def toString() = { for (rows <- cells) for (row <- rows) Console.println(row) } } I don't understand why I get this error: error: type mismatch; found : Unit required: java.lang.String for (rows <- cells) Why is "cells" now a Unit? And why does it require a String for a for loop?
-
Answer:
I figured it out, by the way. The problem is that by default, the "for" loop returns type Unit. If you want it to return an array, then you have to use yield. Also, I found that for was a bad choice for a toString method anyway. My new code is this: override def toString: String = "[[" + cells.map(_.mkString(", ")).mkString("]\n [") + "]]\n"
Luke Shepard at Quora Visit the source
Related Q & A:
- Why do I keep getting booted out of Yahoo IM?Best solution by Yahoo! Answers
- Why do I keep getting disconnected from YM?Best solution by answers.yahoo.com
- Why do I keep getting back email that I sent failure?Best solution by Yahoo! Answers
- Why do I keep getting an error message when I use Yahoo?Best solution by Yahoo! Answers
- Why I keep getting an error messenger when i try to down load yahoo messenger though?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.