|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Announcing Scala.js 0.6.14 |
| 4 | +category: news |
| 5 | +tags: [releases] |
| 6 | +permalink: /news/2016/12/21/announcing-scalajs-0.6.14/ |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +We are excited to announce the release of Scala.js 0.6.14! |
| 11 | + |
| 12 | +This release features a few language enhancements for interoperability. |
| 13 | +Among others, it allows to export top-level functions, and provides language support for JavaScript "configuration objects". |
| 14 | + |
| 15 | +<!--more--> |
| 16 | + |
| 17 | +## Getting started |
| 18 | + |
| 19 | +If you are new to Scala.js, head over to |
| 20 | +[the tutorial]({{ BASE_PATH }}/tutorial/). |
| 21 | + |
| 22 | +## Release notes |
| 23 | + |
| 24 | +If upgrading from Scala.js 0.6.12 or earlier, make sure to read [the release notes of 0.6.13]({{ BASE_PATH }}/news/2016/10/17/announcing-scalajs-0.6.13/), which contain some breaking changes in sbt build definitions. |
| 25 | + |
| 26 | +As a minor release, 0.6.14 is backward source and binary compatible with previous releases in the 0.6.x series. |
| 27 | +Libraries compiled with earlier versions can be used with 0.6.14 without change. |
| 28 | +However, it is not forward compatible: libraries compiled with 0.6.14 cannot be used by projects using 0.6.{0-13}. |
| 29 | + |
| 30 | +Please report any issues [on GitHub](https://github.com/scala-js/scala-js/issues). |
| 31 | + |
| 32 | +## JavaScript configuration objects |
| 33 | + |
| 34 | +Many JavaScript libraries expose APIs where options are given in what we call configuration objects. |
| 35 | +A well-known example would be the [`$.ajax()`](http://api.jquery.com/jquery.ajax/) function of jQuery, which we can declare as: |
| 36 | + |
| 37 | +{% highlight scala %} |
| 38 | +@ScalaJSDefined |
| 39 | +trait JQueryAjaxSettings extends js.Object { |
| 40 | + val data: js.Object | String | js.Array[Any] |
| 41 | + val contentType: Boolean | String |
| 42 | + val crossDomain: Boolean |
| 43 | + val success: js.Function3[Any, String, JQueryXHR, _] |
| 44 | + ... |
| 45 | +} |
| 46 | + |
| 47 | +def ajax(url: String, settings: JQueryAjaxSettings): JQueryXHR = js.native |
| 48 | +{% endhighlight %} |
| 49 | + |
| 50 | +The `val`s need to be declared abstract because, in previous versions of Scala.js, `@ScalaJSDefined` traits could not have concrete members. |
| 51 | +This makes it tedious to create an instance of `JQueryAjaxSettings`, since the Scala type system forces us to fill them in, although the fields are optional in JavaScript. |
| 52 | +Previous workarounds include a) using `js.Dynamic.literal(...).asInstanceOf[JQueryAjaxSettings]` or b) using [`JSOptionBuilder`](https://github.com/jducoeur/jsext#jsoptionbuilder). |
| 53 | + |
| 54 | +Scala.js 0.6.14 finally provides language support for this kind of API. |
| 55 | +It is now allowed to have concrete members in `@ScalaJSDefined` traits, *if* their right-hand-side is `= js.undefined` (this implies that their type must be a supertype of `js.UndefOr[Nothing]`). |
| 56 | +With this relaxed rule, we can define `JQueryAjaxSettings` as follows: |
| 57 | + |
| 58 | +{% highlight scala %} |
| 59 | +@ScalaJSDefined |
| 60 | +trait JQueryAjaxSettings extends js.Object { |
| 61 | + val data: js.UndefOr[js.Object | String | js.Array[Any]] = js.undefined |
| 62 | + val contentType: js.UndefOr[Boolean | String] = js.undefined |
| 63 | + val crossDomain: js.UndefOr[Boolean] = js.undefined |
| 64 | + val success: js.UndefOr[js.Function3[Any, String, JQueryXHR, _]] = js.undefined |
| 65 | + ... |
| 66 | +} |
| 67 | +{% endhighlight %} |
| 68 | + |
| 69 | +When calling `ajax()`, we can now give an anonymous object that overrides only the `val`s we care about: |
| 70 | + |
| 71 | +{% highlight scala %} |
| 72 | +jQuery.ajax(someURL, new JQueryAjaxSettings { |
| 73 | + override val crossDomain: js.UndefOr[Boolean] = true |
| 74 | + override val success: js.UndefOr[js.Function3[Any, String, JQueryXHR, _]] = { |
| 75 | + js.defined { (data: Any, textStatus: String, xhr: JQueryXHR) => |
| 76 | + println("Status: " + textStatus) |
| 77 | + } |
| 78 | + } |
| 79 | +}) |
| 80 | +{% endhighlight %} |
| 81 | + |
| 82 | +Note that for functions, we use `js.defined { ... }` to drive Scala's type inference. |
| 83 | +Otherwise, it needs to apply two implicit conversions, which is not allowed. |
| 84 | + |
| 85 | +The explicit types are still quite annoying, but they are only necessary in Scala 2.10 and 2.11. |
| 86 | +If you use Scala 2.12, you can omit all the type annotations (but keep `js.defined`), thanks to improved type inference for `val`s and SAM conversions: |
| 87 | + |
| 88 | +{% highlight scala %} |
| 89 | +jQuery.ajax(someURL, new JQueryAjaxSettings { |
| 90 | + override val crossDomain = true |
| 91 | + override val success = js.defined { (data, textStatus, xhr) => |
| 92 | + println("Status: " + textStatus) |
| 93 | + } |
| 94 | +}) |
| 95 | +{% endhighlight %} |
| 96 | + |
| 97 | +## Export top-level functions |
| 98 | + |
| 99 | +In Scala.js 0.6.13 and earlier, the only way to export something resembling a top-level function was to export the method and its enclosing object, as follows: |
| 100 | + |
| 101 | +{% highlight scala %} |
| 102 | +@JSExport |
| 103 | +object Main { |
| 104 | + @JSExport |
| 105 | + def main(): Unit = { ... } |
| 106 | +} |
| 107 | +{% endhighlight %} |
| 108 | + |
| 109 | +However, this requires to call it as `Main().main()` from JavaScript, which shows that it is not actually a top-level function. |
| 110 | + |
| 111 | +In Scala.js 0.6.14, you can use the `@JSExportTopLevel` annotation instead: |
| 112 | + |
| 113 | +{% highlight scala %} |
| 114 | +object Main { |
| 115 | + @JSExportTopLevel("main") |
| 116 | + def main(): Unit = { ... } |
| 117 | +} |
| 118 | +{% endhighlight %} |
| 119 | + |
| 120 | +which allows to call `main()` from JavaScript. |
| 121 | +`@JSExportTopLevel` methods must be declared in top-level `object`s. |
| 122 | + |
| 123 | +## Bug fixes |
| 124 | + |
| 125 | +Among others, the following bugs have been fixed in 0.6.14: |
| 126 | + |
| 127 | +* [#2587](https://github.com/scala-js/scala-js/issues/2587) Incorrect result for `BigDecimal` multiplication (corner case) |
| 128 | +* [#2639](https://github.com/scala-js/scala-js/issues/2639) The linker should refuse to build if `@JSImport` is used with `NoModule` |
| 129 | +* [#2640](https://github.com/scala-js/scala-js/issues/2640) `TimeoutException` when running PhantomJS with macOS Sierra |
| 130 | +* [#2643](https://github.com/scala-js/scala-js/issues/2643) `js.ThisFunction` passing Scala-`this` instead of JS-`this` when using Scala 2.12.0 |
| 131 | +* [#2655](https://github.com/scala-js/scala-js/issues/2655) Inconsistent behavior in `NumericRange.min` method between JVM and JS |
| 132 | +* [#2689](https://github.com/scala-js/scala-js/issues/2689) The optimizer does something wrong with `return` inside `finally` |
| 133 | + |
| 134 | +You can find the full list [on GitHub](https://github.com/scala-js/scala-js/issues?q=is%3Aissue+milestone%3Av0.6.14+is%3Aclosed). |
0 commit comments