• Stars
    star
    65
  • Rank 473,702 (Top 10 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library that toggles Scala code at compile-time, like #if in C/C++

enableIf.scala

Join the chat at https://gitter.im/ThoughtWorksInc/enableIf.scala Build Status Latest version Scaladoc

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 classs or objects via fully quantified names from dependency libraries
  • The boolean condition been evaluated must not refer other classs or objects 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.

Enable different code for Apache Spark 3.1.x and 3.2.x

For breaking API changes of 3rd-party libraries, simply annotate the target method with the artifactId and the version to make it compatible.

To distinguish Apache Spark 3.1.x and 3.2.x:

object XYZ {
  @enableIf(classpathMatches(".*spark-catalyst_2\\.\\d+-3\\.2\\..*".r))
  private def getFuncName(f: UnresolvedFunction): String = {
    // For Spark 3.2.x
    f.nameParts.last
  }
  
  @enableIf(classpathMatches(".*spark-catalyst_2\\.\\d+-3\\.1\\..*".r))
  private def getFuncName(f: UnresolvedFunction): String = {
    // For Spark 3.1.x
    f.name.funcName
  }
}

For specific Apache Spark versions:

@enableIf(classpathMatchesArtifact(crossScalaBinaryVersion("spark-catalyst"), "3.2.1"))
@enableIf(classpathMatchesArtifact(crossScalaBinaryVersion("spark-catalyst"), "3.1.2"))

NOTICE: classpathMatchesArtifact is for classpath without classifiers. For classpath with classifiers like ffmpeg-5.0-1.5.7-android-arm-gpl.jar, Please use classpathMactches or classpathContains.

Hints to show the full classpath:

sbt "show Compile / fullClasspath"

mill show foo.compileClasspath

More Repositories

1

Binding.scala

Reactive data-binding for Scala
Scala
1,579
star
2

DeepLearning.scala

A simple library for creating complex neural networks
Scala
766
star
3

DeepDarkFantasy

A Programming Language for Deep Learning
Haskell
465
star
4

cd4ml-workshop

Repository with sample code and instructions for "Continuous Intelligence" and "Continuous Delivery for Machine Learning: CD4ML" workshops
Jupyter Notebook
316
star
5

Dsl.scala

A framework to create embedded Domain-Specific Languages in Scala
Scala
255
star
6

each

A macro library that converts native imperative syntax to scalaz's monadic expressions
Scala
253
star
7

guia-de-desenvolvimento-tecnico

JavaScript
206
star
8

Compute.scala

Scientific computing with N-dimensional arrays
Scala
199
star
9

CD4ML-Scenarios

Repository with sample code and instructions for "Continuous Intelligence" and "Continuous Delivery for Machine Learning: CD4ML" workshops
Python
138
star
10

microbuilder

A toolset that helps you build system across multiple micro-services and multiple languages.
HTML
93
star
11

sbt-api-mappings

An Sbt plugin that fills apiMappings for common Scala libraries.
Scala
88
star
12

todo

Binding.scala • TodoMVC
Scala
60
star
13

sinais

🔣 Desenvolvimento passo a passo do exemplo `sinais` em Go.
Go
59
star
14

sbt-best-practice

Configure common build settings for a Scala project
Scala
56
star
15

TWU101-TDDIntro

Java
46
star
16

template.scala

C++ Flavored Template Metaprogramming in Scala
Scala
40
star
17

future.scala

Stack-safe asynchronous programming
Scala
39
star
18

ml-app-template

An ML project template with sensible defaults
Python
37
star
19

sbt-scala-js-map

A Sbt plugin that configures source mapping for Scala.js projects hosted on Github
Scala
36
star
20

aws_role_credentials

Generates AWS credentials for roles using STS
Python
34
star
21

transervicos

Ruby
33
star
22

RAII.scala

Resource Acquisition Is Initialization
Scala
32
star
23

sbt-example

Run Scaladoc as unit tests
Scala
31
star
24

feature.scala

Access Scala language features on the type-level
Scala
31
star
25

sbt-ammonite-classpath

Export the classpath for Ammonite and Almond
Scala
27
star
26

Import.scala

A Scala compiler plugin for magic imports
Scala
26
star
27

JS-Monthly-Chengdu

CSS
23
star
28

infra-problem

resources for the infrastructure as code practical assessment
Clojure
23
star
29

bindable.scala

User-friendly Binding.scala components
Scala
23
star
30

implicit-dependent-type

Scala
22
star
31

Extractor.scala

Make PartialFunction and extractors composable
Scala
22
star
32

ml-cd-starter-kit

Set up cross-cutting services (e.g. CI server, monitoring) for ML projects using kubernetes and helm
Smarty
22
star
33

objective8

For the most up to date version of this project, see https://github.com/d-cent/objective8
Clojure
21
star
34

oktaauth

Module and CLI client to handle Okta authentication
Python
20
star
35

js-test-project

JavaScript
18
star
36

tryt.scala

Monad transformers for exception handling
Scala
18
star
37

lein-s3-static-deploy

Lein task to deploy static website to s3 bucket.
Clojure
17
star
38

DesignPattern.scala

Functional Programming Design Patterns
Scala
17
star
39

daffy

Function decorators for Pandas Dataframe column name and data type validation
Python
15
star
40

TWTraining

Open source ThoughtWorks training materials
HTML
15
star
41

dsl-domains-cats

Scala
12
star
42

Q.scala

Convert any value to code
Scala
12
star
43

ZeroCost.scala

Zero-cost Abstractions in Scala
Scala
10
star
44

Constructor.scala

Mixin classes and traits dynamically
Scala
10
star
45

dataclouds

Blog for dataclouds@thoughtworks.
CSS
10
star
46

tf-image-interpreter

Object detection and text spotting from images of any size. Based on TensorFlow.
Python
10
star
47

WorkingEffectivelyWithLegacyCode

Java
10
star
48

Binding.scala-website

Scala
9
star
49

voter-service

The Voter Spring Boot RESTful Web Service, backed by MongoDB, is used for DevOps-related training and testing.
Java
9
star
50

OpenStack-EC2-Driver

OpenStack-EC2-Driver
9
star
51

stonecutter

[Main repo found at https://github.com/d-cent/stonecutter] A D-CENT project: an easily deployable oauth server for small organisations.
Clojure
9
star
52

streaming-data-pipeline

Streaming pipeline repo for data engineering training program
Scala
9
star
53

JavaBootcamp

Java
8
star
54

java-test-project

Java
7
star
55

infra-code-workshop

TechRadar Academy em PoA - Cloud
7
star
56

twseleniumworkshop

Workshop Selenium Belo Horizonte - Setembro 2014
Java
7
star
57

DeepLearning.scala-website

The website of DeepLearning.scala
CSS
7
star
58

AS101-4-workshop

Python
7
star
59

skadoosh

Here we have the building blocks of a virtual entity in the making (in crude words, a chat bot - but don't call it that. It gets offended).
Python
7
star
60

json-stream-core

Universal Serialization Framework for JSON
Haxe
6
star
61

ScaleWorks_YUMChina

Ruby
6
star
62

microbuilder-core

Haxe
6
star
63

clj-http-s3

Middleware to allow cli-http to authenticate with s3
Clojure
6
star
64

lein-filegen

A leiningen plugin to generate files
Clojure
6
star
65

expend-rs

Internal application to submit certain expenses to ThoughtWorks' system
Rust
6
star
66

LatestEvent.scala

bidirectional data-binding and routing for Scala.js
Scala
6
star
67

sbt-jdeps

an sbt plugin to run JDeps
Scala
5
star
68

modularizer

Scala
5
star
69

twu-toolkit

Calendar generator for TWU
Ruby
5
star
70

twcss

CSS Coding Guidelines
5
star
71

loans-lah-tdd-workshop

JavaScript
5
star
72

Binding.scala-play-template

Scala
5
star
73

TypeOf.scala

Create types from expressions
Scala
4
star
74

SG-ObjectBootcamp

Java
4
star
75

sbt-delombok

an sbt plug-in to delombok Java sources files that contain Lombok annotations
Scala
4
star
76

zeratul

a wrapper for JPA
Java
4
star
77

CSharpTestProject

C#
4
star
78

HashRoute.scala

Scala
4
star
79

Tensor.scala

A totally functional DSL for general purpose GPU programming
4
star
80

akka-http-rpc

Turn akka-http to a RPC server
Scala
4
star
81

monadic-deep-learning

TeX
4
star
82

wxapp-workshop

3
star
83

sde

Scala
3
star
84

clojuregoat

A goat, in Clojure
Clojure
3
star
85

go-maven-poller

Go plugin that polls Maven (Nexus) repositories
3
star
86

sonic

React UI Components
JavaScript
3
star
87

mooncake

A D-CENT project: Secure notifications combined with w3 activity streams
Clojure
3
star
88

Binding.scala-activator-template

Scala
3
star
89

offnet

The Unified Neural Network
Jupyter Notebook
3
star
90

akka-http-webjars

Serve static assets from WebJars
Scala
3
star
91

scala-project-template

3
star
92

aem-training-2016

Code repo for AEM training in August 2016.
Java
3
star
93

helsinki

[Main repo found at https://github.com/d-cent/decisionsproto] Spike for indexing data from the Open Ahjo API in elasticsearch
Python
3
star
94

hackerbrasileiro

Java
3
star
95

FallbackLookupStrategy.java

Java
2
star
96

cep-conference

Core Engineering Practices "Conference" exercise
Java
2
star
97

Cifar10.scala

Scala
2
star
98

infra-code-devopslabs01

TechRadar Academy - Cloud Workshop - IaC
Python
2
star
99

Kaleidoscopez

JavaScript
2
star
100

android-test-project

Kotlin
2
star