Scala basic calculation

62 views
Skip to first unread message

James Ong

unread,
Jul 12, 2012, 9:24:50 AM7/12/12
to play-fr...@googlegroups.com
I found this SO question only explain the code example for Java, tried Scala turn out a little different:

What wrong with the getSum code?
type mismatch; found : Int required: String 


object Application extends Controller {
  
  def index = Action {
    Ok(views.html.index("1","2",Application.getSum("1","2")))  //<- Error type mismatch
  }
  
  def getSum(a: String,b: String):Int = Action {
val c = a.toInt + b.toInt;
  } c
}
Message has been deleted

Marcel Klemenz

unread,
Jul 12, 2012, 9:42:46 AM7/12/12
to play-fr...@googlegroups.com
Or do you try to pass String String Int of your template requires String String String?
would make more sense.

James Ong

unread,
Jul 12, 2012, 9:44:38 AM7/12/12
to play-fr...@googlegroups.com
Oh, I spotted my index template is a String, So rewrite as
getSum("1","2").toString)

Will solve the type issue.

James Ong

unread,
Jul 12, 2012, 9:50:27 AM7/12/12
to play-fr...@googlegroups.com
For the quick working example for Scala, any tips to optimize the code below will be appreciated!

object Application extends Controller {
  
  def index = Action {
    Ok(views.html.index("1","2",getSum("1","2").toString))
  }
  
  var c:Int = 0;
  def getSum(a: String,b: String):String = {
c = a.toInt + b.toInt
c.toString    ///return c as String
  }
}
Reply all
Reply to author
Forward
0 new messages