intro to scala js
play

Intro to Scala.js Singapore-Scala, 28 Mar 2017 Li Haoyi - PowerPoint PPT Presentation

Intro to Scala.js Singapore-Scala, 28 Mar 2017 Li Haoyi haoyi.sg@gmail.com Bright Technology Services www.bright.sg About me Previously software engineer at Dropbox Currently at Bright technology services (www.bright.sg) -


  1. Intro to Scala.js Singapore-Scala, 28 Mar 2017 Li Haoyi haoyi.sg@gmail.com Bright Technology Services www.bright.sg

  2. About me Previously software engineer at Dropbox Currently at Bright technology services (www.bright.sg) - data-science/software consulting Early contributor to Scala.js, author of Ammonite REPL, Scalatags, FastParse, ... @li_haoyi on Twitter, @lihaoyi on Github

  3. Agenda What is this Scala.js thing? Why should I care? How does it work?

  4. Compiling to Javascript

  5. Timeline of Compile-to-Javascript Languages Dart Websharper Elm Haxe Clojurescript Opal PyJS 2006 2009 2012 2014 2015 2017 Google Coffeescript Typescript Scala.js Babel Web Toolkit

  6. Why Compile to Javascript?

  7. Why Compile to Javascript? Share expertise between Client and Server Share code between Client and Server Choose what language to build your website in

  8. Scala.js www.scala-js.org

  9. Scala.js: What def main () = { var x = 0 while( x < 999 ){ x = x + "2" . toInt } println ( x ) }

  10. Scala.js: What ScalaJS.c.LExample$.prototype.main__V = ( function () { var x = 0; while ((x < 999)) { x = ((x + new ScalaJS.c.sci_StringOps().init___T( ScalaJS.m.s_Predef().augmentString__T__T("2") ).toInt__I()) | 0) }; ScalaJS.m.s_Predef().println__O__V(x) });

  11. Scala.js: What be.prototype.main =function (){ for ( var a = 0;999 > a;) a = a + ( new de).g(S(L(),"2")).ne() | 0; ee(); L(); var b = F(fe); ge(); a = ( new he).g(w(a)); b = bc(0,J(q(b,[a]))); ie(bc(L(),J(q(F(fe),[je(ke(ge().Vg),b)])))) }

  12. Scala.js: Examples Ray Tracing scalafiddle.io/sf/4beVrVc/1 Online Games www.lihaoyi.com/roll Web Apps demo.fluentcode.com

  13. Common patterns of using Scala.js Client-side Scala.js Client-Server Scala/Scala.js

  14. Client-side Scala.js

  15. Live Demo: Client-side Scala.js github.com/lihaoyi/workbench-example-app

  16. Type-checked by default var paragraph = document.body console.log(paragraph.childdern.length) ScalaJSExample.scala:12: value childrren is not a member of org.scalajs.dom.raw.Element console.log(paragraph.childrren.length) ^ Compilation failed

  17. Fewer Warts javascript> ["10", "10", "10", "10"].map(parseInt) [10, NaN, 2, 3] // WTF scalajs> Seq("10", "10", "10", "10").map(Integer.parseInt) List(10, 10, 10, 10)

  18. Great editor support

  19. Library Ecosystem Use any JS library - val xhr = new XMLHttpRequest() - React - D3 - … Along with lots of Scala libraries...

  20. Client-side Scala.js: Limitations Can use: Can’t use: • Most of java.lang.* • j.l.Thread, j.l.Runtime, ... • Almost all of scala.* • s.c.parallel, s.tools.nsc • Some of java.util.* • org.omg.CORBA, sun. misc.* • Scala Macros: upickle, scala-async, • Reflection: scala-pickling, scalaxy, etc scala-reflect • Pure-Scala ecosystem: shapeless, • Java-dependent: Scalatest, Scalate scalaz, scalatags, utest

  21. Client-side Scala.js: Limitations Can use: Can’t use: • JS stuff: XmlHttpRequest, • JVM stuff: Netty, akka, spray, file Websockets. Localstorage IO, JNI • HTML DOM, Canvas, WebGL • AWT, Swing, SWT, OpenGL • JavaScript libraries: chipmunk.js, • Java ecosystem: guice, junit, hand.js, react.js, jquery apache-commons, log4j • IntelliJ, Eclipse, SBT • Yourkit, VisualVM, JProfiler • Chrome console, firebug

  22. Client-side Scala.js: using Javascript libraries // Chipmunk.js definition in javascript // Chipmunk.js definition in Scala cp.Vect = function(x, y){ package cp this.x = x; @JSName("cp.Vect") this.y = y; class Vect(var x: Double, }; var y: Double) extends js.Object // using Chipmunk.js in javascript // using Chipmunk.js in Scala var p = new cp.Vect(50, 100) val p = new cp.Vect(50, 100) console.log(p.x + p.y) // 150 println(p.x + p.y) // 150

  23. Client-Server Scala.js

  24. Embed Scala in your JS Server Web Client

  25. Embed Scala in your JS Foo.scala Server Web Client

  26. Embed Scala in your JS @JSExportTopLevel("Foo") var foo = new Foo(3); class Foo(val x: Int) { @JSExport console.log(foo.square()); def square(): Int = x*x // 9 @JSExport("foobar") def add(y: Int): Int = x+y console.log(foo.foobar(5)); } // 8

  27. Embed Scala in your JS NetLogo Web: agent-based simulation engine marianogappa/ostinato: re-usable chess engine fommil.com/kerbal: kerbal space program calculator

  28. Isomorphic Javascript - Code - Libraries - Conventions Shared Client Server

  29. Isomorphic Scala Shared Client Server

  30. Scalatags, uPickle, Scalatest React Akka-HTTP Angular javax.mail jQuery Scopt Shared Client Server

  31. Isomorphic Scala: Shared Libraries Scalaz: functional programming Scalatags: HTML templating Scalatest: test framework uPickle: JSON serialization Shapeless: generic programming BooPickle: binary serialization Akka: actors Circe: JSON handling Accord: data validation Autowire: type-safe routing Monix: asynchronous streaming RosHTTP: HTTP client Scala Async: async & await FastParse: parser combinators QuickLens: lenses for updating case classes Shocon: Typesafe “HOCON” config parser

  32. Why Scala.js?

  33. What do Javascript developers get from Scala.js? Everything in ES6/7 (=>, destructuring, string-interpolation, ...) Everything in TypeScript (types, generics, ...) Everything in Immutable.js (immutable collections) + Great language, extensive standard library, functional programming, shared client-server code, access to all JS libs, fearless refactoring

  34. What do Scala developers get from Scala.js? You can already write back-end servers And compilers You can now program web front-ends, Browser extensions Node.js servers: github.com/scalajs-io/nodejs Microcontrollers (www.espruino.com, tessel.io) Mobile apps with React-Native

  35. Scala.js: Performance - Relatively quick: 1-2s warm turnaround - Acceptable size: small apps start at ~70kb, grow to 100s of kb pre-gzip - Efficient Code: ~1-2x slower than “raw” Javascript

  36. Intro to Scala.js scala-js.org haoyi.sg@gmail.com www.bright.sg

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend