Scala Warrior
● A game written in Scala for learning Scala
● Inspired by Ruby Warrior
● Based on Scala.js, ScalaTags and ScalaCSS
https://github.com/scalawarrior/scalawarrior
Scala学習用のWebベースのゲームです。
Scala.js、ScalaTags、ScalaCSSなどを使用しています。
Motivation
● We wish Scala to become more popular
programming language in Japan
● Activities we did for that since 2011:
○ Writing Scala books
○ Making hands-on tutorial for Play2 and Slick
○ Using Scala in real business systems
2011年頃からScalaの普及のための様々な活動を行ってきま
した。
Making learning fun
● Newbies are feeling
difficulty in Scala
more than necessary
● In fact, We can learn
Scala step by step
Scala初心者はScalaを必要以上に難しく感じています。
もっと楽しく気軽に学ぶことのできる方法が必要です。
Server
(Play2)
Architecture of Scala Warrior
Web Browser
Scala
2. Compile Scala code to
JavaScript by Scala.js compiler
1. Send Scala code edited by user
サーバでScala.jsコンパイラでJavaScriptコードを生成
Server
(Play2)
Architecture of Scala Warrior
Web Browser
Scala
JavaScript
2. Compile Scala code to
JavaScript by Scala.js compiler
1. Send Scala code edited by user
生成したJavaScriptコードをブラウザに返却
Server
(Play2)
Architecture of Scala Warrior
Web Browser
Scala
JavaScript
2. Compile Scala code to
JavaScript by Scala.js compiler
1. Send Scala code edited by user
3. Evaluate responded JavaScript code
ブラウザ側で返却されたJavaScriptをeval()して評価
Made their Scala.js facade for Scala.js
● scalajs-ace
○ Ace is a powerful online code editor
○ But Scala Warrior hasn't used for several reasons
● scalajs-createjs
○ CreateJS is a suite of libraries to build rich
interactive web contents
これらのライブラリのScala.js用ファサードを作りました(でも
Scala Warriorではscalajs-aceは使っていません)
Great thanks for Li Haoyi's scala-js-fiddle!
https://github.com/lihaoyi/scala-js-fiddle
Li Haoyiさんのscala-js-fiddleを参考にさせていただきました。
ありがとうございます。
Note
● Scala Warrior is a distinctive application
taking advantage of Scala.js
● Uses Scala.js to compile Scala code
entered by user
● Evaluates compiled code on the browser to
protect the server
Scala Warriorは実行時にScala.jsコンパイラを活用する特殊
なアプリケーションであることに注意してください。
Scala.js is universal!
Play project (server-side)
Scala.js project (client-side)
Cross-build project (common)
Scalaとコードを共有することもできます。
Interoperability
● Need type mapping like .d.ts
○ scala-js-ts-importer can convert from .d.ts
○ You can find a lot of .d.ts at DefinitelyTyped
○ But can't convert perfectly...
● js.Dynamic (not type-safe)
import scala.scalajs.js.Dynamic._
val editor: Dynamic = global.ace.edit("editor")
JavaScriptとの連携も完璧です。
ScalaTags
● Type-safe XML/HTML generation library
● Scala and Scala.js support
ScalaTagsはXMLやHTMLをScalaコードでタイプセーフに生
成するためのライブラリです。
● Add this line to build.sbt
● @JSExport annotated elements are
expoted as CommonJS module
Export Scala.js as CommonJS module
scalaJSModuleKind := ModuleKind.CommonJSModule
CommonJSモジュールとしてエクスポートできます。
Export Scala.js as CommonJS module
import scala.scalajs.js
import js.annotation._
@ScalaJSDefined
@JSExport("HelloWorld")
class HelloWorld extends js.Object {
def sayHello(name: String): String = s"Hello ${name}!"
}
var hello = require("./hello-fastopt.js")
var helloWorld = new hello.HelloWorld();
console.log(helloWorld.sayHello("Scala.js"));
Scala.js
JavaScript
Use CommonJS module in Scala.js
import scala.scalajs.js
import js.annotation._
@js.native
@JSImport("./hello-fastopt.js", "HelloWorld")
class HelloWorld extends js.Object {
def sayHello(name: String): String = js.native
}
Scala.jsでCommonJSモジュールを使うこともできます。
Why Scala in frontend?
● Scala.js is not ALL or NOTHING!
○ Scala programmers can provide libraries for
frontend using Scala.js
○ JavaScript programmer can use existing frontend
tools and frameworks
全部Scala.jsで作らないといけないわけではありません。
適切な役割分担によってScalaのメリットを活かせます。