earldouglas / linear-scala   0.0.3

ISC License GitHub

Linear types in Scala

Scala versions: 2.13 2.12 2.11
sbt plugins: 1.x

Build Status Release Artifacts

linear-scala

linear-scala adds support for linear types in Scala via a custom Scalafix linter.

Setup

project/plugins.sbt:

addSbtPlugin("com.earldouglas" % "sbt-linear-scala" % "0.0.3")

Usage

Mix in the Linear interface to prevent values from being under/over-used.

import com.earldouglas.linearscala.Linear

case class Box(value: Int) extends Linear

Scalafix finds values that are never used:

trait UnusedField {
  val box: Box = Box(42) // error: box is never used
}

trait UnusedParameter {
  def foo(x: Box, y: Box): Int = // error: y is never used
    x.value
}

Scalafix also finds values that are used multiple times:

trait FieldUsedTwice {
  val box: Box = Box(42)
  println(box) // error: box is used twice
  println(box) // error: box is used twice
}

See the tests in input/src/main/scala/fix/ for more examples.

References

Linear Types