Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on May 3, 2019. It is now read-only.

mpilquist/local-implicits

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Local Implicits

This project contains a Scala compiler plugin which adds support for locally scoped implicit values.

For example:

imply("Hello, world!") { implicitly[String] }

expands to:

{
  implicit val implied$0 = "Hello, world!"
  implicitly[String]
}

which at runtime, evaluates to "Hello, world!".

The imply method takes two parameter lists -- the first parameter list contains 1 or more values to declare in a local implicit scope, and the second parameter list contains a block to evaluate in context of the local implicit scope.

Motivation

Defining implicits in local scope with vanilla Scala is problematic for a number of reasons.

  • In some cases, opening a new block requires either a semi-colon on the line before the start of the block, or an empty line before the start of the block.
  • Arbitrary names have to be assigned to each implicit.
  • The resulting code is rather verbose considering its function.

The imply method is particularly useful when working with type class instances that have multiple lawful implementations. For example, consider some monoid instances for Int:

2 |+| 3                              // 5
imply(intMultiplication) { 2 |+| 3 } // 6
imply(minMonoid) { 2 |+| 3 }         // 2
imply(maxMonoid) { 2 |+| 3 }         // 3

Usage

This plugin currently supports Scala 2.10 and 2.11.

To use this plugin with SBT, add the following to build.sbt:

addCompilerPlugin("com.github.mpilquist" %% "local-implicits" % "0.3.0")

About

Scala compiler plugin which provides syntax for working with locally declared implicit values

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.