Download - Scala Frustrations

Transcript
Page 1: Scala Frustrations

Scala Frustrations

in Real Development Naoki Takezoe

@takezoen

NTT-DATA INTELLILINK

Page 2: Scala Frustrations

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.

Page 3: Scala Frustrations

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.

Page 4: Scala Frustrations

Why we are using Scala?

• Decrease costs of system development.

• We felt the limit of Java several years ago.

• Scala might be a breakthrough?

Page 5: Scala Frustrations

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

Page 6: Scala Frustrations

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

Page 7: Scala Frustrations

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

Page 8: Scala Frustrations
Page 9: Scala Frustrations

Long compilation time

• Compilation is so much heavy.

• Painful for large application development.

Page 10: Scala Frustrations

Solution

Buy high spec machines

Asynchronous and automated build

Page 11: Scala Frustrations

ScalaIDE is heavy

• We meet a sandglass frequently.

Page 12: Scala Frustrations

Solution

1. Turn off the incremental builder

2. Turn off the code completion

3. Try other IDE or text editor

Page 13: Scala Frustrations

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.

Page 14: Scala Frustrations

Solution

We really need SBT in Action!

Page 15: Scala Frustrations
Page 16: Scala Frustrations

Long compilation time

• Many code generation and template compilation.

• Development mode is slow also.

Page 17: Scala Frustrations

Solution

Buy high spec machines

Divide large project

play2-fastassets decreases request

Page 18: Scala Frustrations

Inflexible Validation

• Client-side validation is not provided.

• Error Message can’t be contained field name.

Page 19: Scala Frustrations

Solution

Original client-side validation framework based on Play2’s form definition

Original helper to display error messages

Page 20: Scala Frustrations

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.

Page 21: Scala Frustrations

Solution

Use other ORMs such as ScalaQuery.

We used a combination of scalaquery-magic scalagen and mirage-scala.

Page 22: Scala Frustrations

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

Page 23: Scala Frustrations

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) )

Page 24: Scala Frustrations

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.

Page 25: Scala Frustrations

Solution

play2-war-plugin packs Play2 apps to war.

play2-httpsession provides HttpSession by using with play2-war-plugin.

Page 26: Scala Frustrations
Page 27: Scala Frustrations

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

Page 28: Scala Frustrations

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

Page 29: Scala Frustrations

• 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

Page 30: Scala Frustrations

• 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

Page 31: Scala Frustrations

• 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

Page 32: Scala Frustrations

• 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

Page 33: Scala Frustrations
Page 34: Scala Frustrations

Scala is practical

• You are evangelists of Scala.

• Please introduce Scala in your work.

• But so carefully, especially in the large project.

Page 35: Scala Frustrations

Scala is now glowing up!

• This way along which Java passed.

• Time will solve these problems.

• We can contribute Scala glowing.

Page 36: Scala Frustrations

Top Related