Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ktoso/scala-macro-method-alias
Scala 2.10 macros that help you delegate to other methods declaratively
https://github.com/ktoso/scala-macro-method-alias
Last synced: 3 months ago
JSON representation
Scala 2.10 macros that help you delegate to other methods declaratively
- Host: GitHub
- URL: https://github.com/ktoso/scala-macro-method-alias
- Owner: ktoso
- License: apache-2.0
- Created: 2013-02-09T18:12:17.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-02-17T19:13:35.000Z (almost 11 years ago)
- Last Synced: 2024-04-16T10:21:15.106Z (9 months ago)
- Language: Scala
- Size: 469 KB
- Stars: 16
- Watchers: 6
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
scala macro method alias
========================
*scala macro method alias* makes it possible to delegate to other methods without actually writing the delegation code.Background
----------
The idea came up during a Scala User Group meetup after JFokus (Stockholm) where we were discussing how Akka has
to maintain 2 APIs at the same time - an example would be `tell` and `!` (where `tell` is referred to as "Java API").
√ictor jokingly said that a `@delegate` annotation would be fun, I came back to this idea while me and my friend were waiting for our plane back home to Poland. Turns out it's doable and doesn't even look all too bad - see for yourself.So to keep it in one sentence: This macro allows you to easily (without the boilerplate of passing around the input arguments) delegate from one method to another. Imagine you're supporting multiple API which do the same thing, or you've deprecated one of the methods and now keep routing it to the new implementation.
Disclaimer
==========
Macros are not the best solution when slapped around everywhere - in this case, I believe the win we get from it is pretty low, so I wouldn't advertise using this macro oeverywhere just because it's cool ;-)Usage
=====
The project is synced with Sonatype OSS as well as Maven Central.Dependency in *SBT* format:
```scala
libraryDependencies += "pl.project13.scala" %% "macro-method-alias" % "1.0"
```Dependency in *Maven* format:
```xml
pl.project13.scala
macro-method-alias_2.10
1.0```
Examples
========First, import the macro:
```scala
import pl.project13.scala.macros.Alias._
```Suggested style: aliasFor method
--------------------------------
There are two ways to delegate using macro-delegate. One of them is the `aliasFor` method:```scala
// delegation code will be generated automaticallydef tell(name: String) = ???
def !(name: String) = aliasFor { tell _ }
```![](docs/delegate1.png)
![](docs/delegate2.png)
Delegation of up to 22 params is supported.
Annotation style: @aliasFor + delegated[ReturnType]
---------------------------------------------------
The other style allows you to define the delegate as annotation - which makes it more visible and also documentable that this method will only
delegate calls to the other implementation.It has the downside that the implementation of this method has to explicitly type the return type, as in: `delegated[Int]`.
![](docs/delegate3.png)
![](docs/delegate4.png)
Nice error messages
-------------------
I tried to make it as safe as possible - thus the macro will abort compilation if it detects the types of the
delegator and delegate don't match, or if you forget to include the annotation when using `delegated` mode:Error when you forget to add the `@aliasFor` annotation when using `delegated`:
![](docs/compile_error.png)Error when you type the wrong type into `delegated`:
![](docs/type_error.png)But well, the safest way is to use the *suggested style* anyway... :-)
Required Scala version
======================* Scala 2.10+
License
=======
**Apache 2 License**