├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.sbt ├── core └── src │ ├── main │ ├── scala-2.10 │ │ └── io │ │ │ └── travisbrown │ │ │ └── abstracted │ │ │ └── internal │ │ │ └── MacrosCompat.scala │ ├── scala-2.11 │ │ └── io │ │ │ └── travisbrown │ │ │ └── abstracted │ │ │ └── internal │ │ │ └── MacrosCompat.scala │ └── scala │ │ └── io │ │ └── travisbrown │ │ └── abstracted │ │ ├── Converter.scala │ │ ├── Empty.scala │ │ └── package.scala │ └── test │ └── scala │ └── io │ └── travisbrown │ └── abstracted │ └── AbstractedSpec.scala ├── demo └── src │ └── main │ └── scala │ └── io │ └── travisbrown │ └── abstracted │ └── demo │ ├── Box.scala │ └── Finagle.scala ├── project ├── build.properties └── plugins.sbt └── version.sbt /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | target/ 3 | .idea/ 4 | .idea_modules/ 5 | .DS_STORE 6 | .cache 7 | .settings 8 | .project 9 | .classpath -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: scala 2 | sudo: false 3 | 4 | scala: 5 | - 2.10.6 6 | - 2.11.7 7 | 8 | jdk: 9 | - openjdk7 10 | - oraclejdk7 11 | - oraclejdk8 12 | 13 | script: sbt ++$TRAVIS_SCALA_VERSION clean coverage test coverageReport 14 | 15 | after_success: bash <(curl -s https://codecov.io/bash) 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Abstracted 2 | 3 | [![Build status](https://img.shields.io/travis/travisbrown/abstracted/master.svg)](http://travis-ci.org/travisbrown/abstracted) 4 | [![Coverage status](https://img.shields.io/codecov/c/github/travisbrown/iteratee/master.svg)](https://codecov.io/github/travisbrown/abstracted) 5 | [![Maven Central](https://img.shields.io/maven-central/v/io.travisbrown/abstracted_2.11.svg)](https://maven-badges.herokuapp.com/maven-central/io.travisbrown/abstracted_2.11) 6 | 7 | This is a small proof of concept that demonstrates how to implement a Scala 8 | macro that allows us to "forget" all of a value's methods and only use 9 | enrichment methods (which will usually be provided via a type class). 10 | 11 | The idea behind `abstracted` was originally (as far as I know) suggested by 12 | [Michael Pilquist](https://twitter.com/mpilquist) in the 13 | [cats](https://github.com/non/cats) room on 14 | [Gitter](https://gitter.im/non/cats?at=5565ecf27a71f1612c266c8d), although the 15 | approach he suggests is different from the one I've used here. 16 | 17 | ## Simple example 18 | 19 | As an example, suppose we've got a `Box` type: 20 | 21 | ```scala 22 | case class Box[A](val a: A) { 23 | def map[B](f: A => B): Box[B] = { 24 | println("Box's map") 25 | Box(f(a)) 26 | } 27 | 28 | def flatMap[B](f: A => Box[B]): Box[B] = { 29 | println("Box's flatMap") 30 | f(a) 31 | } 32 | } 33 | ``` 34 | 35 | And a monad instance for it: 36 | 37 | ```scala 38 | import cats.Monad 39 | 40 | implicit val boxMonad: Monad[Box] = new Monad[Box] { 41 | override def map[A, B](fa: Box[A])(f: A => B): Box[B] = { 42 | println("Box's functor's map") 43 | Box(f(fa.a)) 44 | } 45 | 46 | def flatMap[A, B](fa: Box[A])(f: A => Box[B]): Box[B] = { 47 | println("Box's monad's flatMap") 48 | f(fa.a) 49 | } 50 | 51 | def pure[A](a: A): Box[A] = Box(a) 52 | } 53 | ``` 54 | 55 | Now if we use `Box` in a `for`-comprehension, for example, the monad instance 56 | won't get used: 57 | 58 | ```scala 59 | scala> import io.travisbrown.abstracted.demo._ 60 | import io.travisbrown.abstracted.demo._ 61 | 62 | scala> import cats.syntax.all._ 63 | import cats.syntax.all._ 64 | 65 | scala> for { foo <- Box("foo"); howMany <- Box(3) } yield foo * howMany 66 | Box's flatMap 67 | Box's map 68 | res0: io.travisbrown.abstracted.demo.Box[String] = Box(foofoofoo) 69 | ``` 70 | 71 | Our `abstracted` macro allows us to change this: 72 | 73 | ```scala 74 | scala> import io.travisbrown.abstracted._ 75 | import io.travisbrown.abstracted._ 76 | 77 | scala> for { foo <- Box("foo").abstracted; howMany <- Box(3) } yield foo * howMany 78 | Box's monad's flatMap 79 | Box's map 80 | res1: io.travisbrown.abstracted.demo.Box[String] = Box(foofoofoo) 81 | ``` 82 | 83 | ## Finagle services 84 | 85 | I decided to take a stab at implementing `abstracted` tonight because of 86 | [a conversation about how Finagle services compose](https://github.com/twitter/finagle/issues/385) 87 | this afternoon. Finagle services are morally more or less Kleisli arrows over 88 | Twitter futures, but for whatever reason `Service` extends `I => Future[O]`, 89 | which means that they have totally useless `compose` and `andThen` methods. In 90 | another [project](https://github.com/travisbrown/catbird) I provide category 91 | and profunctor instances for `Service`, but the `compose` and `andThen` 92 | enrichment methods provided by cats for things with `Compose` instances are 93 | blocked by the stupid methods that `Service` inherits from `Function1`. 94 | 95 | For example, if we've got these services: 96 | 97 | ```scala 98 | import cats.syntax.compose._ 99 | import com.twitter.util.Future 100 | import com.twitter.finagle.Service 101 | import io.catbird.finagle._ 102 | 103 | val is = Service.mk[Int, String](i => Future.value(i.toString)) 104 | val si = Service.mk[String, Int](s => Future(s.toInt)) 105 | ``` 106 | 107 | We get an error when we try to compose them: 108 | 109 | ```scala 110 | scala> val ss = si andThen is 111 | :22: error: type mismatch; 112 | found : com.twitter.finagle.Service[Int,String] 113 | required: com.twitter.util.Future[Int] => ? 114 | si andThen is 115 | ^ 116 | ``` 117 | 118 | Our `abstracted` macro fixes this problem: 119 | 120 | ```scala 121 | scala> import io.travisbrown.abstracted._ 122 | import io.travisbrown.abstracted._ 123 | 124 | scala> val ss = si.abstracted andThen is 125 | ss: com.twitter.finagle.Service[String,String] = 126 | ``` 127 | 128 | ## How it works 129 | 130 | The implementation is pretty straightforward. First we've got an implicit class 131 | that provides a `def abstracted: Empty[A]` method for any `A`, where our `Empty` 132 | type is a case class that wraps an `A` and provides access to the wrapped value, 133 | but doesn't have any other methods. 134 | 135 | We also have a `Converter[A, B]` type that represents a conversion from 136 | `Empty[A]` to `B` (I ran into problems trying to use `Empty[A] => B` directly), 137 | and an implicit method that will apply the conversion automatically to any 138 | `Empty[A]` for any appropriately-typed `Converter` instance. 139 | 140 | The interesting part is how we make `Converter` instances. We use the Scala 141 | macro system's [fundep materialization](http://docs.scala-lang.org/overviews/macros/implicits.html), which allows us to determine in the body of the macro what the output 142 | type of the `Converter` will be. We look at the open implicits and find one that 143 | looks like the compiler is fishing for a `WhateverOps` enrichment class for our 144 | `Empty[A]`. We then ask for a view from `A` (our real type) to the target of 145 | that view. We read the return type off the view from `A`, and from there the 146 | implementation is pretty trivial. 147 | 148 | ## Status 149 | 150 | It seems like it works. The examples above can be run by opening up a REPL with 151 | `sbt demo/console`. If other people think it looks useful I guess it could end 152 | up in cats, although there's nothing cats-specific about the macro itself or the 153 | surrounding machinery. 154 | 155 | ## License 156 | 157 | abstracted is licensed under the **[Apache License, Version 2.0][apache]** (the 158 | "License"); you may not use this software except in compliance with the License. 159 | 160 | Unless required by applicable law or agreed to in writing, software 161 | distributed under the License is distributed on an "AS IS" BASIS, 162 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 163 | See the License for the specific language governing permissions and 164 | limitations under the License. 165 | 166 | [apache]: http://www.apache.org/licenses/LICENSE-2.0 167 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | lazy val sharedSettings = Seq( 2 | organization := "io.travisbrown", 3 | scalaVersion := "2.11.7", 4 | crossScalaVersions := Seq("2.10.6", "2.11.7"), 5 | libraryDependencies ++= Seq( 6 | "org.scala-lang" % "scala-compiler" % scalaVersion.value, 7 | "org.scalatest" %% "scalatest" % "2.2.5" % "test" 8 | ), 9 | libraryDependencies ++= ( 10 | CrossVersion.partialVersion(scalaVersion.value) match { 11 | case Some((2, 10)) => Seq( 12 | compilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full), 13 | "org.scalamacros" %% "quasiquotes" % "2.1.0" 14 | ) 15 | case _ => Nil 16 | } 17 | ), 18 | ScoverageSbtPlugin.ScoverageKeys.coverageHighlighting := ( 19 | CrossVersion.partialVersion(scalaVersion.value) match { 20 | case Some((2, 10)) => false 21 | case _ => true 22 | } 23 | ) 24 | ) 25 | 26 | lazy val root = project.in(file(".")) 27 | .settings(sharedSettings ++ publishSettings) 28 | .settings(noPublishSettings) 29 | .aggregate(core, demo) 30 | .dependsOn(core) 31 | 32 | lazy val core = project 33 | .settings( 34 | description := "abstracted: forget your instance methods", 35 | moduleName := "abstracted", 36 | name := "abstracted" 37 | ) 38 | .settings(sharedSettings ++ publishSettings) 39 | .settings( 40 | libraryDependencies ++= Seq( 41 | /** 42 | * We use Scalaz only in our tests. 43 | */ 44 | "org.scalaz" %% "scalaz-core" % "7.2.0" % "test" 45 | ) 46 | ) 47 | 48 | lazy val demo = project 49 | .settings(moduleName := "abstracted-demo") 50 | .settings(sharedSettings ++ noPublishSettings) 51 | .settings( 52 | resolvers += Resolver.sonatypeRepo("snapshots"), 53 | libraryDependencies += "io.catbird" %% "catbird-finagle" % "0.2.0" 54 | ) 55 | .dependsOn(core) 56 | 57 | lazy val publishSettings = Seq( 58 | releaseCrossBuild := true, 59 | releasePublishArtifactsAction := PgpKeys.publishSigned.value, 60 | homepage := Some(url("https://github.com/travisbrown/abstracted")), 61 | licenses := Seq("Apache 2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")), 62 | publishMavenStyle := true, 63 | publishArtifact in Test := false, 64 | pomIncludeRepository := { _ => false }, 65 | publishTo := { 66 | val nexus = "https://oss.sonatype.org/" 67 | if (isSnapshot.value) 68 | Some("snapshots" at nexus + "content/repositories/snapshots") 69 | else 70 | Some("releases" at nexus + "service/local/staging/deploy/maven2") 71 | }, 72 | scmInfo := Some( 73 | ScmInfo( 74 | url("https://github.com/travisbrown/abstracted"), 75 | "scm:git:git@github.com:travisbrown/abstracted.git" 76 | ) 77 | ), 78 | pomExtra := ( 79 | 80 | 81 | travisbrown 82 | Travis Brown 83 | https://twitter.com/travisbrown 84 | 85 | 86 | ), 87 | credentials ++= ( 88 | for { 89 | username <- Option(System.getenv().get("SONATYPE_USERNAME")) 90 | password <- Option(System.getenv().get("SONATYPE_PASSWORD")) 91 | } yield Credentials( 92 | "Sonatype Nexus Repository Manager", 93 | "oss.sonatype.org", 94 | username, 95 | password 96 | ) 97 | ).toSeq 98 | ) 99 | 100 | lazy val noPublishSettings = Seq( 101 | publish := (), 102 | publishLocal := (), 103 | publishArtifact := false 104 | ) 105 | -------------------------------------------------------------------------------- /core/src/main/scala-2.10/io/travisbrown/abstracted/internal/MacrosCompat.scala: -------------------------------------------------------------------------------- 1 | package io.travisbrown.abstracted.internal 2 | 3 | import scala.reflect.ClassTag 4 | 5 | private[abstracted] trait MacrosCompat { 6 | type Context = scala.reflect.macros.Context 7 | 8 | def resultType(c: Context)(tpe: c.Type)(implicit 9 | /** 10 | * See SI-5143 for discussion of why we need this class tag. 11 | */ 12 | tag: ClassTag[c.universe.MethodType] 13 | ): c.Type = { 14 | import c.universe.MethodType 15 | 16 | tpe match { 17 | case MethodType(_, res) => resultType(c)(res) 18 | case other => other 19 | } 20 | } 21 | 22 | def implicitViews(c: Context)(source: c.Type)(implicit 23 | /** 24 | * See SI-5143 for discussion of why we need this class tag. 25 | */ 26 | tag: ClassTag[c.universe.TypeRef] 27 | ): List[c.Tree] = { 28 | import c.universe.{ EmptyTree, TypeRef } 29 | 30 | c.enclosingImplicits.collect { 31 | case (TypeRef(_, _, _ :: target :: _), _) => 32 | c.inferImplicitView(EmptyTree, source, target) 33 | }.filterNot(_.isEmpty) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /core/src/main/scala-2.11/io/travisbrown/abstracted/internal/MacrosCompat.scala: -------------------------------------------------------------------------------- 1 | package io.travisbrown.abstracted.internal 2 | 3 | import scala.reflect.ClassTag 4 | 5 | private[abstracted] trait MacrosCompat { 6 | type Context = scala.reflect.macros.whitebox.Context 7 | 8 | def resultType(c: Context)(tpe: c.Type): c.Type = tpe.finalResultType 9 | 10 | def implicitViews(c: Context)(source: c.Type)(implicit 11 | /** 12 | * See SI-5143 for discussion of why we need this class tag. 13 | */ 14 | tag: ClassTag[c.universe.TypeRef] 15 | ): List[c.Tree] = { 16 | import c.ImplicitCandidate 17 | import c.universe.{ EmptyTree, TypeRef } 18 | 19 | c.enclosingImplicits.collect { 20 | case ImplicitCandidate(_, _, TypeRef(_, _, _ :: target :: _), _) => 21 | c.inferImplicitView(EmptyTree, source, target) 22 | }.filterNot(_.isEmpty) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /core/src/main/scala/io/travisbrown/abstracted/Converter.scala: -------------------------------------------------------------------------------- 1 | package io.travisbrown.abstracted 2 | 3 | import scala.language.experimental.macros 4 | 5 | /** 6 | * A conversion from a wrapped value to some other type. 7 | */ 8 | trait Converter[A, B] { 9 | def apply(a: Empty[A]): B 10 | } 11 | 12 | private[abstracted] final object Converter extends internal.MacrosCompat { 13 | implicit def materialize[A, B]: Converter[A, B] = macro materializeImpl[A, B] 14 | 15 | def materializeImpl[A, B](c: Context)(implicit 16 | A: c.WeakTypeTag[A] 17 | ): c.Expr[Converter[A, B]] = { 18 | import c.universe._ 19 | 20 | val view = implicitViews(c)(A.tpe) match { 21 | case List(unique) => unique 22 | case _ => 23 | c.abort(c.enclosingPosition, s"Unable to find appropriate view for $A") 24 | } 25 | 26 | c.Expr[Converter[A, B]]( 27 | q""" 28 | new Converter[$A, ${ resultType(c)(view.tpe) }] { 29 | final def apply(e: Empty[$A]) = $view(e.a) 30 | } 31 | """ 32 | ) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /core/src/main/scala/io/travisbrown/abstracted/Empty.scala: -------------------------------------------------------------------------------- 1 | package io.travisbrown.abstracted 2 | 3 | /** 4 | * Wraps a value but doesn't have any other methods. 5 | */ 6 | case class Empty[A](a: A) 7 | 8 | private[abstracted] final object Empty { 9 | implicit def convert[A, B](e: Empty[A])(implicit converter: Converter[A, B]): B = converter(e) 10 | } 11 | -------------------------------------------------------------------------------- /core/src/main/scala/io/travisbrown/abstracted/package.scala: -------------------------------------------------------------------------------- 1 | package io.travisbrown 2 | 3 | package object abstracted { 4 | implicit final class Abstracted[A](val a: A) { 5 | final def abstracted: Empty[A] = Empty(a) 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /core/src/test/scala/io/travisbrown/abstracted/AbstractedSpec.scala: -------------------------------------------------------------------------------- 1 | package io.travisbrown.abstracted 2 | 3 | import org.scalatest.{ Matchers, FlatSpec } 4 | 5 | class AbstractedSpec extends FlatSpec with Matchers { 6 | val numbers = (1 to 10).toList 7 | val incremented = numbers.map(_ + 1) 8 | 9 | "The abstracted macro" should "provide enrichment methods" in { 10 | import scalaz.std.list._, scalaz.syntax.functor._ 11 | 12 | val fromScalazFunctorOps: List[(Int, Int)] = numbers.abstracted.fpair 13 | 14 | fromScalazFunctorOps shouldBe numbers.map(i => (i, i)) 15 | } 16 | 17 | it should "provide enrichment methods hidden by ordinary methods" in { 18 | val fromStdLibMonadOps: TraversableOnce[Int] = numbers.abstracted.map(_ + 1) 19 | 20 | fromStdLibMonadOps.toSeq should contain theSameElementsInOrderAs incremented 21 | } 22 | 23 | it should "hide ordinary methods not provided through enrichment" in { 24 | import scalaz.std.list._, scalaz.syntax.monad._ 25 | 26 | "numbers.abstracted.size" shouldNot compile 27 | } 28 | 29 | it should "not provided methods that are neither ordinary nor enriched" in { 30 | import scalaz.std.list._, scalaz.syntax.monad._ 31 | 32 | "numbers.abstracted.nonsense" shouldNot compile 33 | } 34 | 35 | it should "prefer methods from more specific enrichment classes" in { 36 | import scalaz.std.list._, scalaz.syntax.functor._ 37 | 38 | val fromScalazFunctorOps: List[Int] = numbers.abstracted.map(_ + 1) 39 | 40 | fromScalazFunctorOps shouldBe incremented 41 | } 42 | 43 | it should "work with for-comprehensions" in { 44 | import scalaz.std.list._, scalaz.syntax.monad._ 45 | 46 | val result = for { 47 | i <- numbers 48 | j <- numbers 49 | } yield i + j 50 | 51 | val fromScalazMonadOps: List[Int] = for { 52 | i <- numbers.abstracted 53 | j <- numbers.abstracted 54 | } yield i + j 55 | 56 | fromScalazMonadOps shouldBe result 57 | } 58 | 59 | it should "work with filtered for-comprehensions" in { 60 | /** 61 | * Note that we need `MonadPlusOps` in order to be able to filter. Otherwise 62 | * we would get the `withFilter` provided by the standard library's 63 | * `MonadOps`, which doesn't return the appropriate type. 64 | */ 65 | import scalaz.std.list._, scalaz.syntax.monadPlus._ 66 | 67 | val result = for { 68 | i <- numbers 69 | j <- numbers 70 | if i > 8 71 | } yield i + j 72 | 73 | val fromScalazMonadOps: List[Int] = for { 74 | i <- numbers.abstracted 75 | j <- numbers.abstracted 76 | if i > 8 77 | } yield i + j 78 | 79 | fromScalazMonadOps shouldBe result 80 | } 81 | 82 | it should "not compile if not all operations are available" in { 83 | /** 84 | * These imports provide `map` and `flatMap`, but not `withFilter`. 85 | */ 86 | import scalaz.std.list._, scalaz.syntax.monad._ 87 | 88 | """ 89 | for { 90 | i <- numbers.abstracted 91 | j <- numbers.abstracted 92 | if i > 8 93 | } yield i + j 94 | """ shouldNot compile 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /demo/src/main/scala/io/travisbrown/abstracted/demo/Box.scala: -------------------------------------------------------------------------------- 1 | package io.travisbrown.abstracted.demo 2 | 3 | import cats.Monad 4 | 5 | case class Box[A](val a: A) { 6 | def map[B](f: A => B): Box[B] = { 7 | println("Box's map") 8 | Box(f(a)) 9 | } 10 | 11 | def flatMap[B](f: A => Box[B]): Box[B] = { 12 | println("Box's flatMap") 13 | f(a) 14 | } 15 | } 16 | 17 | object Box { 18 | implicit val boxMonad: Monad[Box] = new Monad[Box] { 19 | override def map[A, B](fa: Box[A])(f: A => B): Box[B] = { 20 | println("Box's functor's map") 21 | Box(f(fa.a)) 22 | } 23 | 24 | def flatMap[A, B](fa: Box[A])(f: A => Box[B]): Box[B] = { 25 | println("Box's monad's flatMap") 26 | f(fa.a) 27 | } 28 | 29 | def pure[A](a: A): Box[A] = Box(a) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /demo/src/main/scala/io/travisbrown/abstracted/demo/Finagle.scala: -------------------------------------------------------------------------------- 1 | package io.travisbrown.abstracted.demo 2 | 3 | import cats.syntax.compose._ 4 | import com.twitter.util.Future 5 | import com.twitter.finagle.Service 6 | import io.catbird.finagle._ 7 | import io.travisbrown.abstracted._ 8 | 9 | object Finagle { 10 | val i2s = Service.mk[Int, String](i => Future.value(i.toString)) 11 | val s2i = Service.mk[String, Int](s => Future(s.toInt)) 12 | 13 | val s2s = s2i.abstracted.andThen(i2s) 14 | } 15 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.9 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.0") 2 | addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") 3 | addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.1.0") 4 | -------------------------------------------------------------------------------- /version.sbt: -------------------------------------------------------------------------------- 1 | version in ThisBuild := "0.2.0-SNAPSHOT" --------------------------------------------------------------------------------