What are some examples of how Scala's compiler plugins are used in practice?
-
-
Answer:
We currently have 2 compiler plugins that we use at Quora, both written by (one with help from Scala compiler maintainer Paul Phillips as a consultant) and with some minor modifications by me. These allow us to get some very useful features of Python that we wanted by extending Scala's syntax with these plugins. AutoArgs. This plugin lets us get something similar to *args and **kwargs for certain Scala classes. We use this for very quick and efficient serialization of classes, and in a way also backwards-compatible with our Python code. We enable this plugin by mixing in this trait: trait AutoArgs { def autoArgs: Array[Any] = Array() // filled by compiler plugin def autoArgNames: Array[String] = Array() // filled by compiler plugin def autoKwargs: Map[String, Any] = Map((autoArgNames zip autoArgs): _*) } and we then make use of the autoArgs and autoKwargs defs in the class or its consumers. Decorate. This plugin lets us implement the functionality of Python decorators [1] with Scala's static annotations. We declare our own FunctionDecorator abstract class that extends from scala.annotation.StaticAnnotation. Any decorator we want to write can then just inherit from FunctionDecorator and implement a single method decorate. The compiler plugin fills in the rest, including support for decorator stacking. This allows function composition in the same simple and readable fashion as Python's syntax: @Count @Memoize(persistToDatabase = false) def fib(num: Int): Int = { // ... } In general though, we prefer to build functionality using Scala natively where possible. (We have resisted many ideas that we would want to implement with a compiler plugin...) But there are times where the benefits of code simplicity & maintainability outweigh the cost of writing these compiler plugins, and we will make that trade-off. [1] http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators
Kah Seng Tay at Quora Visit the source
Other answers
The delimited continuations plugin is the most used plugin... I'm not sure how much "practice" there is. The ScalaQL plugin is toyed with by some, but not used in practice. Kevin Wright has a plugin that allows the "dynamic" addition of a trait to an existing class instance at run-time. And of course, the Goat Rodeo plugin is used far and wide in my office https://www.assembla.com/code/goat_rodeo/git/nodes/rodeo-plugin/src/main/scala/org/goatrodeo/plugin?rev=42f71448be1d21ba940af3e963c248d85f359b83
David Pollak
Related Q & A:
- What are natural examples of non-relativizable proofs?Best solution by Theoretical Computer Science
- What are some examples of motif?Best solution by answers.com
- How to add Yahoo plugins?Best solution by Yahoo! Answers
- How much are pool sticks worth? In it's own pouch/case? (used?Best solution by Yahoo! Answers
- What are the examples of how Lady Macbeth is violent, aggressive, and uncontrollable?Best solution by 123helpme.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.