Scala Frustrations in Real Development Naoki Takezoe @takezoen
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?
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 Flexibility and Safety ▫ More type safe ▫ Immutable data types • Java Interoperability ▫ Many Java based Frameworks and Libraries are available ▫ Easy to port existing Java software
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, ... Max 18 properties "tel“
scalagen https://github.com/takezoe/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 = "" )
scalaquery-magic https://github.com/shimamoto/scalaquery-magic • 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)
mirage-scala https://github.com/takezoe/mirage-scala • 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*/