Scala Frustrations
-
Upload
takezoe -
Category
Technology
-
view
3.269 -
download
3
description
Transcript of Scala Frustrations
Scala Frustrations
in Real Development Naoki Takezoe
@takezoen
NTT-DATA INTELLILINK
Abstract
• Scala is pretty good for real development.
• However Scala has also some bad points.
• Please note for these points when you introduce Scala into real development.
Scala 300 Recipes from Shoeisha
• Includes 300 Practical Scala Recipes.
• from Basic Scala topics to frameworks and tools.
Covers Play2, Akka, sbt, scaladoc, ScalaIDE, ScalaTest, Specs2, Scalatra, ScalaQuery, Anorm, Casbah, spray, scala-io, scala-time, sjson, util-eval, scalaxb, Dispatch and more.
Why we are using Scala?
• Decrease costs of system development.
• We felt the limit of Java several years ago.
• Scala might be a breakthrough?
About Our Project
• Porting Java based web application to Scala.
• Over 170 HTMLs and 40,000 lines.
• 5 members on 6 months.
Before Seasar2 Apache Click S2JDBC PostgreSQL + Tsearch Raw JavaScript
After Play2 (Customized) ScalaQuery (Cuztomized) PostgreSQL Apache Solr jQuery + jQuery UI
Benefits of Scala
• Decrease amount of source code ▫ 40%-50% OFF ▫ Better abstraction techniques
• Decrease bugs ▫ More type safe ▫ Immutable data types
• Java Interoperability ▫ Many Java based Frameworks and Libraries are
available ▫ Easy to port existing Java software
Benefits of Scala
• Decrease amount of source code ▫ 40%-50% OFF ▫ Better abstraction techniques
• Decrease bugs ▫ More type safe ▫ Immutable data types
• Java Interoperability ▫ Many Java based Frameworks and Libraries are
available ▫ Easy to port existing Java software
Flexibility and Safety
Long compilation time
• Compilation is so much heavy.
• Painful for large application development.
Solution
Buy high spec machines
Asynchronous and automated build
ScalaIDE is heavy
• We meet a sandglass frequently.
Solution
1. Turn off the incremental builder
2. Turn off the code completion
3. Try other IDE or text editor
sbt is NOT Simple Build Tool
• When we get a trouble, difficult to find a reason.
• Rapid version up causes compatibility problem of sbt plugins.
Solution
We really need SBT in Action!
Long compilation time
• Many code generation and template compilation.
• Development mode is slow also.
Solution
Buy high spec machines
Divide large project
play2-fastassets decreases request
Inflexible Validation
• Client-side validation is not provided.
• Error Message can’t be contained field name.
Solution
Original client-side validation framework based on Play2’s form definition
Original helper to display error messages
Anorm is too simple
• We have to write all SQL.
• Magic does not already exist.
• SQL is not type safe.
• No support for dynamic SQL.
Solution
Use other ORMs such as ScalaQuery.
We used a combination of scalaquery-magic scalagen and mirage-scala.
Function22 Problem in form definition
• a.k.a. Tuple22 Problem
• Form can not have over 18 properties.
val userForm = Form( mapping( "firstName“ -> text, "lastName“ -> text, "mailAddress“ -> email, "password“ -> text, ... "tel“ -> text, "mobile“ -> text, "company“ -> text, "department“ -> text )(UserInfo.apply)(UserInfo.unapply) )
Max 18 properties
Solution
Nested definition, BUT it’s not expectable.
val userForm = Form( mapping( "firstName" -> text, "lastName" -> text, "mailAddress" -> email, "password" -> text, "companyInfo" -> mapping( "company" -> text, "department" -> text )(CompanyInfo.apply)(CompanyInfo.unapply) )(UserInfo.apply)(UserInfo.unapply) )
No Servlet and HttpSession
• Hard to port existing Java based web apps.
• We want to run Play2 on the servlet container by un-technical reasons.
Solution
play2-war-plugin packs Play2 apps to war.
play2-httpsession provides HttpSession by using with play2-war-plugin.
solr-scala-client
• Simple wrapper of SolrJ for Scala.
• Query converter using parser combinator.
import jp.sf.amateras.solr.scala._ val client = new SolrClient("http://localhost:8983/solr") val result = client.query("name: ?name?") .getResultAsMap(Map("name" -> "ThinkPad & X201s")) // => name:("ThinkPad" AND "X201s") result.documents.foreach { doc: Map[String, Any] => ... }
https://github.com/takezoe/solr-scala-client
scalagen
• Code Generator from RDBMS. • Supports Anorm and ScalaQuery in the current version. • Easy to add support for other ORMs. • Work as CLI and sbt plugin.
seq(jp.sf.amateras.scalagen.ScalagenPlugin.scalagenSettings: _*) scalagenConfiguration := jp.sf.amateras.scalagen.Settings( // for ScalaQuery generator = new jp.sf.amateras.scalagen.ScalaQueryGenerator(), // for Anorm //generator = new jp.sf.amateras.scalagen.ScalaQueryGenerator(), driver = "org.hsqldb.jdbcDriver", url = "jdbc:hsqldb:hsql://localhost/", username = "sa", password = "" )
https://github.com/takezoe/scalagen
• Extends ScalaQuery.
• Make a Magic (like old Anorm’s Magic) by code generation using scalagen.
val userInfoDao = new UserInfoDao() // SELECT by ID val userInfo = userInfoDao.selectById(1) // INSERT userInfoDao.insert(UserInfo(DEFAULT[Int], 'userName', 'password')) // UPDATE userInfoDao.update(userInfo.copy(userName = 'newName')) // DELETE by ID userInfoDao.deleteById(1)
scalaquery-magic https://github.com/shimamoto/scalaquery-magic
• SQL Centric ORM similar to Anorm.
• Executable SQL template called 2waySQL.
SLEECT USER_ID, USER_NAME, PASSWORD FROM USER_INFO /*BEGIN*/ WHERE /*IF userId != null*/ USER_ID = /*userId*/1 /*END*/ /*IF userId != null*/ USER_NAME = /*userName*/ 'takezoe' /*END*/ /*END*/
mirage-scala https://github.com/takezoe/mirage-scala
• Provide HttpSession for Play2 applications.
• Work with play2-war-plugin.
import jp.sf.amateras.play2.httpsession.HttpSessionSupport._ def index = Action { implicit request => // retrieve the object from HttpSession val value: Option[String] = HttpSession[String]("key") Ok(value).withHttpSession { // store objects into HttpSession "key" -> "value" } }
play2-httpsession https://github.com/takezoe/play2-httpsession
• Decrease amount of request to external CSS, JavaScript and images by browser cache.
@(title: String)(content: Html) @import jp.sf.amateras.play2.fastassets.FastAssets <!DOCTYPE html> <html> <head> <title>@title</title> <link rel="stylesheet" media="screen" href="@FastAssets.at("stylesheets/main.css")"> <link rel="shortcut icon" type="image/png" href="@FastAssets.at("images/favicon.png")"> <script src="@FastAssets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script> </head> <body> @content </body> </html>
play2-fastassets https://github.com/takezoe/play2-fastassets
Scala is practical
• You are evangelists of Scala.
• Please introduce Scala in your work.
• But so carefully, especially in the large project.
Scala is now glowing up!
• This way along which Java passed.
• Time will solve these problems.
• We can contribute Scala glowing.