scala.js & friends: scala all the things

24
Scala.js & friends SCALA ALL THE THINGS Chris Birchall Content API team

Upload: chris-birchall

Post on 16-Jul-2015

811 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Scala.js & friends: SCALA ALL THE THINGS

Scala.js & friendsSCALA

ALL THETHINGSChris Birchall

Content API team

Page 2: Scala.js & friends: SCALA ALL THE THINGS

Nightwatch

Page 3: Scala.js & friends: SCALA ALL THE THINGS

Nightwatch: a perfectly good app

● Node.js + Server Sent Events● Concise, well written JavaScript● Reliable● Only one problem…

Page 4: Scala.js & friends: SCALA ALL THE THINGS

IT’S NOTWRITTEN IN

SCALA

Page 5: Scala.js & friends: SCALA ALL THE THINGS

Scala.js to the rescue

● Compile Scala code to JS○ Implemented as a compiler plugin

● Supports almost any Scala code● Interop with 3rd-party JS libs

○ (with a bit of boilerplate)● Generated JS size is not outrageous

○ Hello world is about 100k

Credit: scala-js.org

Page 6: Scala.js & friends: SCALA ALL THE THINGS

Hello world in 2 slides

Page 7: Scala.js & friends: SCALA ALL THE THINGS

Hello world in 2 slides

Page 8: Scala.js & friends: SCALA ALL THE THINGS
Page 9: Scala.js & friends: SCALA ALL THE THINGS

3rd-party JS interop

● Use js.Dynamic○ i.e. throw type safety out of the window○ Quick to write, feels like writing normal JS

● Write a Scala facade for your JS lib○ Typesafe, assuming the facade matches the JS

Page 10: Scala.js & friends: SCALA ALL THE THINGS

js.Dynamic

Page 11: Scala.js & friends: SCALA ALL THE THINGS

Scala facade

Page 12: Scala.js & friends: SCALA ALL THE THINGS

Dependency management

● Facades can be published and shared

● Also support for WebJars● The sbt plugin can write all deps to a single

.js file, à la browserify

libraryDependencies +="be.doeraene" %%% "scalajs-jquery" % "0.8.0"

TRIPLE SQUIGGLE!

<script src=”my-scalajs-app-jsdeps.js”></script><script src=”my-scalajs-app.js”></script>

Page 13: Scala.js & friends: SCALA ALL THE THINGS

Scala.js-ifying Nightwatch

● Untyped hashes → case classes● Scala facades for moment.js, Rickshaw● JS if/else chains → Scala pattern matching● uPickle for JSON deserialization

Page 14: Scala.js & friends: SCALA ALL THE THINGS

Scala.js-ifying NightwatchBefore 106 lines of JS After 216 lines of Scala

Facades

Case classes

Page 15: Scala.js & friends: SCALA ALL THE THINGS

Chained if/else -> pattern match

Before After

Page 16: Scala.js & friends: SCALA ALL THE THINGS

Problems along the way

● Obscure Scala.js bugs○ e.g. .className doesn’t work sometimes

● Unexpected uPickle behaviour● Trial and error getting facades to work

○ Gave up and settled on facade + Dynamic hybrid● Brainmelt from Scala/JS context switching

○ e.g. trying to write Scala strings with single quotes

(All in all, > 5 hours to port 100 lines of code)

Page 17: Scala.js & friends: SCALA ALL THE THINGS

Credit: The Atlantic

Result: Kinda sorta typesafe JavaScript!

Page 18: Scala.js & friends: SCALA ALL THE THINGS

Hang on a sec

What’s this?

CSS?

But...

Page 19: Scala.js & friends: SCALA ALL THE THINGS

IT’S NOTWRITTEN IN

SCALA

Page 20: Scala.js & friends: SCALA ALL THE THINGS

ScalaCSS to the rescue?

Erm, no.

DSL is virtually undocumented

Gave up after a bit of fruitless trial and error.

Not quite ready for primetime.

Page 21: Scala.js & friends: SCALA ALL THE THINGS

On to the next victim

Page 22: Scala.js & friends: SCALA ALL THE THINGS

IT’S NOTWRITTEN IN

SCALA

Page 23: Scala.js & friends: SCALA ALL THE THINGS

There, that’s better

http://lihaoyi.github.io/scalatags/

Page 24: Scala.js & friends: SCALA ALL THE THINGS

Conclusions

Should I use Scala to replace my ...

JavaScript Probably not.Boilerplate and bugs outweigh type safety benefits.

CSS Nope. Just use Sass like everyone else.

HTML Maybe. Scalatags could be useful as a templating lang, to replace e.g. Scalate