exoego / scalajs-types-util   0.3.0

Apache License 2.0 GitHub

Scala.js types utility to facilitate common type transformations

Scala versions: 2.13 2.12
Scala.js versions: 1.x 0.6

scalajs-types-util

Scala.js types utility to facilitate common type transformations

Support matrix

ScalaJS 0.6.28+ ScalaJS 1.x
Scala 2.13 ✔️ (v0.3.0 was final) ✔️
Scala 2.12 ✔️ (v0.3.0 was final) ✔️
Scala 2.11 N/A N/A
Scala 2.10 N/A N/A

How to use

Add below line to your SBT project.

libraryDependencies += "net.exoego" %%% "scalajs-types-util" % "0.3.0"

Factory macro

@Factory macro creates a highly-optimized factory method for JS trait, just like normal case classes. Each factory methods are added to a corresponding companion object (if not exist, created automatically).

JS trait is generally lighter and faster than JS class, since plain old JS object can be trait, but class need extra overheads. However, creating a instance of JS trait in Scala.js is a bit error-prone or verbose. @Factory macro improves the situation !

See how to use it.

import scala.scalajs.js
import net.exoego.scalajs.types.util.Factory

@Factory
trait Foo extends js.Object {
  var x: Int
  var y: js.UndefOr[String]
}

val f = Foo(x = 1)
assert(f.x === 1)
assert(f.y === js.undefined)

Type aliases are also supported.

import scala.scalajs.js
import net.exoego.scalajs.types.util.Factory

@Factory
trait Foo extends js.Object {
  var x: Foo.X
  var y: Foo.Y
}
object Foo {
  type X = Int
  type y = js.UndefOr[String]
}

val f = Foo(x = 1)
assert(f.x === 1)
assert(f.y === js.undefined)

Inlining factory method or not

By default, factory methods will be inlined (marked with @inline annotation), so companion object may be completely removed in fullOptStage. Inlining may reduce the size of generated JS for many cases, but if same factory methods are used many times, may be inlining increase JS size. In such case, you may avoid inlinining by setting inline parameter to false.

@Factory(inline = false)
trait Base extends js.Object {
  var foo: String
}

Limitation

If trait is defined inside object, isTopLevel argument must be false.

object Outer {
    @Factory(isTopLevel = false)
    trait Base extends js.Object {
      var foo: String
    }
}