enableIf.scala alternatives and similar packages
Based on the "Extensions" category.
Alternatively, view enableIf.scala alternatives based on common mentions on social networks and blogs.
-
Twitter Util
General-purpose Scala libraries, including a future implementation and other concurrency tools. -
Cassovary
A Scala library that is designed from the ground up for space efficiency, handling graphs with billions of nodes and edges. -
better-files
Simple, safe and intuitive Scala I/O. better-files is a dependency-free pragmatic thin Scala wrapper around Java NIO. -
Enumeratum
A macro to replace Scala enumerations with a sealed family of case objects. This allows additional checks for the compiler, e.g. for missing cases in a match statement. Has additinal support for Json libraries and the Play framework. -
Scala Graph
A Scala library with basic graph functionality that seamlessly fits into the Scala standard collections library. -
Eff
Extensible effects are an alternative to monad transformers for computing with effects in a functional way. -
Hamsters
A mini Scala utility library. Compatible with functional programming beginners. Featuring validation, monad transformers, HLists, Union types. -
scribe
Practical logging framework that doesn't depend on any other logging framework and can be completely configured programmatically. -
Stateless Future
Asynchronous programming in fully featured Scala syntax. -
Scala Blitz
A library to speed up Scala collection operations by removing runtime overheads during compilation, and a custom data-parallel operation runtime. -
Records for Scala
Labeled records for Scala based on structural refinement types and macros. -
Play monadic actions
A simple scala DSL to allow clean and monadic style for Play! Actions -
Freedsl
A library to implement composable side effects, weaving typeclasses on a wrapping type and the free monad. -
Resolvable
A library to optimize fetching immutable data structures from several endpoints in several formats.
Get performance insights in less than 4 minutes
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest. Visit our partner's website for more details.
Do you think we are missing an alternative of enableIf.scala or a related project?
README
enableIf.scala
enableIf.scala is a library that switches Scala code at compile-time, like #if
in C/C++.
Motivation
Suppose you want to create a library for both Scala 2.10 and Scala 2.11. When you implement the library, you may want to call the flatMap
method on TailRec
. However, the method does not exist on Scala 2.10.
With the help of this library, You can create your own implementation of flatMap
for Scala 2.10 target, and the Scala 2.11 target should still use the flatMap
method implemented by Scala standard library.
Usage
Step 1: Add the library dependency in your build.sbt
// Enable macro annotation by scalac flags for Scala 2.13
scalacOptions ++= {
import Ordering.Implicits._
if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
Seq("-Ymacro-annotations")
} else {
Nil
}
}
// Enable macro annotation by compiler plugins for Scala 2.12
libraryDependencies ++= {
import Ordering.Implicits._
if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
Nil
} else {
Seq(compilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full))
}
}
libraryDependencies += "com.thoughtworks.enableIf" %% "enableif" % "latest.release"
Step 2: Create an implicit class for Scala 2.10
import com.thoughtworks.enableIf
@enableIf(scala.util.Properties.versionNumberString.startsWith("2.10."))
implicit class FlatMapForTailRec[A](underlying: TailRec[A]) {
final def flatMap[B](f: A => TailRec[B]): TailRec[B] = {
tailcall(f(underlying.result))
}
}
The @enableIf
annotation accepts a Boolean
expression that indicates if the FlatMapForTailRec
definition should be compiled. The Boolean
expression is evaluated at compile-time instead of run-time.
Step 3: Call the flatMap
method on your TailRec
import scala.util.control.TailCalls._
def ten = done(10)
def tenPlusOne = ten.flatMap(i => done(i + 1))
assert(tenPlusOne.result == 11)
For Scala 2.10, the expression scala.util.Properties.versionNumberString.startsWith("2.10.")
is evaluated to true
, hence the FlatMapForTailRec
definition will be enabled. As a result, ten.flatMap
will call to flatMap
of the implicit class FlatMapForTailRec
.
For Scala 2.11, the expression scala.util.Properties.versionNumberString.startsWith("2.10.")
is evaluated to false
, hence the FlatMapForTailRec
definition will be disabled. As a result, ten.flatMap
will call the native TailRec.flatmap
.
Limitation
- The
enableIf
annotation does not work for top level traits, classes and objects. - The boolean condition been evaluated must refer
class
s orobject
s via fully quantified names from dependency libraries - The boolean condition been evaluated must not refer other
class
s orobject
s from the same library.
Enable different code for Scala.js and JVM targets
Suppose you want to create a Buffer-like collection, you may want create an ArrayBuffer
for JVM target, and the native js.Array
for Scala.js target.
/**
* Enable members in `Jvm` if no Scala.js plugin is found (i.e. Normal JVM target)
*/
@enableMembersIf(c => !c.compilerSettings.exists(_.matches("""^-Xplugin:.*scalajs-compiler_[0-9\.\-]*\.jar$""")))
private object Jvm {
def newBuffer[A] = collection.mutable.ArrayBuffer.empty[A]
}
/**
* Enable members in `Js` if a Scala.js plugin is found (i.e. Scala.js target)
*/
@enableMembersIf(c => c.compilerSettings.exists(_.matches("""^-Xplugin:.*scalajs-compiler_[0-9\.\-]*\.jar$""")))
private object Js {
@inline def newBuffer[A] = new scalajs.js.Array[A]
@inline implicit final class ReduceToSizeOps[A] @inline()(array: scalajs.js.Array[A]) {
@inline def reduceToSize(newSize: Int) = array.length = newSize
}
}
import Js._
import Jvm._
val optimizedBuffer = newBuffer[Int]
optimizedBuffer += 1
optimizedBuffer += 2
optimizedBuffer += 3
// resolved to native ArrayBuffer.reduceToSize for JVM, implicitly converted to ReduceToSizeOps for Scala.js
optimizedBuffer.reduceToSize(1)
You can define a c
parameter because the enableIf
annotation accepts either a Boolean
expression or a scala.reflect.macros.Context => Boolean
function. You can extract information from the macro context c
.