1

Running a Squeryl call for some data that pulls from multiple locations, but for some reason it returns as a unit. How do I get it to return as an Iterable?

Below is pulling of the data:

/**
   * gets a stream for a particular user
   */
  def getUserStream(userId:Long) {
    User.teamIds(userId).toList.map( (team) =>
      Stream.findByTeam(team,0,5).map( (stream) => 
        List(stream)
      ).flatten
    ).flatten.sortBy(_.id)
  }

And then outputting the data, where results returns as Unit:

Stream.getUserStream(userId) match {
      case results => {
        Ok( generate(results.map( (stream) => Map(
                "id" -> stream.id,
                "model" -> stream.model,
                "time" -> stream.time,
                "content" -> stream.content
                ))
            ) ).as("application/json")
      }
      case _ => Ok("")
    }

My initial guess is one function could return as a None, but wouldn't it just return an empty list?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
crockpotveggies
  • 12,682
  • 12
  • 70
  • 140
  • 1
    possible duplicate of [When to use the equals sign in a Scala method declaration?](http://stackoverflow.com/questions/944111/when-to-use-the-equals-sign-in-a-scala-method-declaration) – om-nom-nom Apr 09 '12 at 04:37

2 Answers2

6

You missing the equal sign before you def getUserStream(userId:Long) method body.

def func(x: Int) { x + 1 } // This will return Unit
def func(x: Int) = { x + 1 } // This will return a Int
Brian Hsu
  • 8,781
  • 3
  • 47
  • 59
0

To add a little bit that may be useful, saying def f(x: Int) {}

Is the equivalent of saying def f(x: Int): Unit = {}

If you do not declare the return type (e.g. def f(x: Int) = {}) the type will be inferred from your method body.

A technique to guarantee that you are returning a certain type is to declare it explicitly. This is something you would do when you want to export a public interface with a certain signature. This is important because if you let the type inferencer do all the work it may expose an abstraction that is more general than you want.

def f(x: Int): List[User] = {} // This will not compile.

stphung
  • 178
  • 8