9

The println in the following code works (with or without toInt)

println("retweets : ", e.getOrElse("retweets", 0).toInt)

top10Tweets(""+e.get("text").get, e.getOrElse("retweets", 0).toInt)

But when I pass it as an argument of a function (as above), it does not work. It says "value toInt is not a member of Any"

When I remove toInt, it says,

    type mismatch;
[error]  found   : Any
[error]  required: Int

e is a Map, as follows,

  def tweetDetails(obj: twitter4j.Status) = {
   Map(
   "id" -> obj.getUser().getId(),
   "screenName" -> obj.getUser().getScreenName(),
   "text" -> obj.getText(),
   "retweets" -> obj.getRetweetCount(),
   "mentions" -> obj.getUserMentionEntities().length)
  }

signature of top10Tweets,

def top10Tweets(tweets: String, retweet_c: Int, mention_c: Int) = {
}
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
user644745
  • 5,673
  • 9
  • 54
  • 80

3 Answers3

8

edit:

Ok, with the new information I would suggest you to create a case class that holds the data instead of using a Map, this way you will preserve type information. I know it is common to use hashes/maps for that in dynamically typed languages, but in statically typed languages as scala data types are the preferred way.

orig:

As I neither know what e is, nor what signature top10Tweets has, I can only assume. But from your code and the error I assume that e is a Map[String, String] and you are trying to get the string representation of an integer for the key "retweets" and convert it to an Int. As a default value you pass in an Int, so the type inferencer infers type Any, because that is the most common super type of String and Int. However Any does not have a toInt method and thus you get the error.

Map("x" -> "2").getOrElse("x", 4).toInt
<console>:8: error: value toInt is not a member of Any
              Map("x" -> "2").getOrElse("x", 4).toInt

Either pass in the default value as String, or convert the value of "retweets" to an Int before, if it exists:

e.get("retweets").map(_.toInt).getOrElse(0)

Anyway a little more information would help to give an accurate answer.

drexin
  • 24,225
  • 4
  • 67
  • 81
  • Thanks. I tried, e.get("retweets").map(_.toInt).getOrElse(0) , but still I am getting "value toInt is not a member of Any".... Is it because e is Map(String, Any) ? I updated the question with relevant code. – user644745 Jul 16 '13 at 10:53
  • Hi Drexin, Thanks. Could you please give me an example about how to use "case class" instead of Map ? – user644745 Jul 16 '13 at 12:27
  • This is pretty basic stuff, so I suggest you to read a good book about scala to learn the basics. Pragramming in Scala by Martin Odersky (the creator of scala) is freely available online. – drexin Jul 16 '13 at 14:29
2

Yes because in Map is "string" -> "string" and You did when getOrElse ( else ) string -> int, thats why its Any.

Map("x" -> 2).getOrElse("x", 4).toInt

works fine or You can:

Map("x" -> "2").getOrElse("x", "4").toInt
daaatz
  • 394
  • 1
  • 2
  • 14
1

The same problem bothers me before you could check below

Map("x" -> 2).getOrElse[Int](x, 4)
Ashok
  • 1,868
  • 6
  • 36
  • 70
Y.fei
  • 19
  • 2