<?xml version="1.0"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Scala Macros Use Cases</title>
    <link>http://scalamacros.org</link>
    <atom:link href="http://scalamacros.org/usecases/rss.xml" rel="self" type="application/rss+xml" />
    <description>Scala Macros Use Cases</description>
    <language>en-us</language>
    <pubDate>Fri, 17 May 2013 23:51:09 PDT</pubDate>
    <lastBuildDate>Fri, 17 May 2013 23:51:09 PDT</lastBuildDate>

    
    <item>
      <title>Use case: Aspect-oriented programming</title>
      <link>http://scalamacros.org/usecases/aspect-oriented-programming.html</link>
      <pubDate>Tue, 15 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/aspect-oriented-programming</guid>
      <description>&lt;p&gt;Object-oriented and functional programming provide quite different and mostly complementing approaches to decomposition,
but there are certain aspects (pun intended) of composition that cannot be readily conveniently in either school of thought.
These traditionally include such crosscutting concerns as logging, authentication/authorization, caching and so on.&lt;/p&gt;

&lt;p&gt;Here is an Emacs Lisp snippet that exemplifies introduction of an advice that gets executed before a certain function
(the snippet is courtesy of GNU Emacs Manual, &lt;a href=&quot;http://www.gnu.org/s/emacs/manual/html_node/elisp/Simple-Advice.html&quot;&gt;
Chapter 17.1 A Simple Advice Example&lt;/a&gt;):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cl&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;defadvice&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;previous-line&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;before&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;next-line-at-end&lt;/span&gt;
                                 &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;amp;optional&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;try-vscroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&amp;quot;Insert an empty line when moving up from the top line.&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;next-line-add-newlines&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
           &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;save-excursion&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;beginning-of-line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;bobp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;progn&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;beginning-of-line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;newline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Similar functionality can be implemented
either by embedding the advice in a lambda parameter of a macro annotation (&lt;code&gt;@before(...) def previousLine = ...&lt;/code&gt;)
or by creating a custom macro annotation that would host the desired domain logic).
However, this comes with a restriction. Unfortunately, due to statically typed nature of Scala it'd be impossible to advice methods from a different compilation unit.&lt;/p&gt;

&lt;p&gt;Also, with the help of annotations on package objects, it might be possible to support, so to say, wildcards for advices, called pointcuts
(e.g. here's a pointcut from AspectJ: &lt;code&gt;pointcut set() : execution(* set*(..)) &amp;&amp; this(Point)&lt;/code&gt;).
However, this possibility is unclear at the moment and is subject to additional brainstorming.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Use case: Type-safe bindings</title>
      <link>http://scalamacros.org/usecases/type-safe-bindings.html</link>
      <pubDate>Sat, 12 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/type-safe-bindings</guid>
      <description>&lt;p&gt;The advent of compile-time metaprogramming in C# gave birth to several interesting techniques that add extra expressivity to the type system.
In this installation, we examine one of these techniques that provides a type-safe way to refer to object/class members.&lt;/p&gt;

&lt;p&gt;One of the natural scenarios that involves type-safe bindings is programming web applications in MVC style.
The following example is taken from a blog post &lt;a href=&quot;http://weblogs.asp.net/seanmcalinden/archive/2009/10/21/strongly-typed-asp-net-mvc-helpers.aspx&quot;&gt;Strongly Typed ASP.Net MVC Helpers&lt;/a&gt; by Sean McAlinden
(take a look at the post if you wonder about the internals of this neat trick):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BeginForm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Update&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Home&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RenderTextBoxFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FirstName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RenderValidationMessageFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FirstName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RenderTextBoxFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LastName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RenderValidationMessageFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LastName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code snippet leverages surrogate lambdas hack to describe model properties that are bound to form widgets.
This works because &lt;code&gt;Render&lt;/code&gt; methods specify the type of the second parameter as an &lt;code&gt;Expression&amp;lt;Func&amp;lt;T, U&amp;gt;&amp;gt;&lt;/code&gt;.
Whenever C# compiler sees a function type in the context when an expression type is required, it performs automatic code lifting
(i.e. transforms the body of the function into an AST).&lt;/p&gt;

&lt;p&gt;The same effect would be achieved if &lt;code&gt;Render&lt;/code&gt; methods were &lt;code&gt;macro defs&lt;/code&gt;.
Since macros are expanded during the compile-time, they always get ASTs that correspond to their arguments, which provides code analysis capabilities.
This topic is discussed in more details in &lt;a href=&quot;/usecases/advanced-domain-specific-languages.html&quot;&gt;&quot;Advanced domain-specific languages&quot;&lt;/a&gt;
and &lt;a href=&quot;/usecases/language-integrated-queries.html&quot;&gt;&quot;Language integrated queries&quot;&lt;/a&gt; case studies.&lt;/p&gt;

&lt;p&gt;If you're inspired by this possibility, you might be delighted to know that similar functionality is already present in Scala since version 2.8 (though it has experimental status).
To see how it works, let's compile and run the following code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;scala.reflect._&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Lift&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baz&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;Function&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
  List&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;LocalValue&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NoSymbol,foo,PrefixedType&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;ThisType&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Class&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Lift&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;,Class&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Lift.Foo&lt;span class=&quot;o&quot;&gt;))))&lt;/span&gt;,
  Select&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
    Ident&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;LocalValue&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NoSymbol,foo,PrefixedType&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;ThisType&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Class&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Lift&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;,Class&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Lift.Foo&lt;span class=&quot;o&quot;&gt;))))&lt;/span&gt;,
    Method&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Lift.Foo.bar,NullaryMethodType&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;PrefixedType&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;ThisType&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Class&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;scala&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;,Class&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;scala.Int&lt;span class=&quot;o&quot;&gt;))))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For more information about Scala reflection API, take a look at the following resources:
&lt;a href=&quot;http://lamp.epfl.ch/teaching/projects/archive/coppel_report.pdf&quot;&gt;Reflecting Scala&lt;/a&gt;,
&lt;a href=&quot;http://gilles.dubochet.ch/publications/2011_dubochet_phd.pdf&quot;&gt;Embedded Domain-Specific Languages Using Libraries and Dynamic Metaprogramming&lt;/a&gt; and
&lt;a href=&quot;https://github.com/scala/scala/tree/master/src/library/scala/reflect/api&quot;&gt;reflection API in the trunk of Scala 2.10&lt;/a&gt;,
or write directly to our mailing list at &lt;a href=&quot;http://groups.google.com/group/scala-user&quot;&gt;http://groups.google.com/group/scala-user&lt;/a&gt;.&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Use case: Advanced domain-specific languages</title>
      <link>http://scalamacros.org/usecases/advanced-domain-specific-languages.html</link>
      <pubDate>Fri, 11 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/7-advanced-domain-specific-languages</guid>
      <description>&lt;p&gt;One of the most important use cases of compile-time metaprogramming involves increasing the area of applicability of embedded domain-specific languages (eDSLs).&lt;/p&gt;

&lt;p&gt;As it was nicely summarized in &lt;a href=&quot;http://biblion.epfl.ch/EPFL/theses/2011/5007/EPFL_TH5007.pdf&quot;&gt;Embedded Domain-Specific Languages using Libraries
and Dynamic Metaprogramming&lt;/a&gt; by Gilles Dubochet, sooner or later DSLs hit the limitations of even very advanced programming languages.
Here's an illustrative example from &lt;a href=&quot;http://squeryl.org/&quot;&gt;Squeryl&lt;/a&gt;, a Scala ORM for SQL databases:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grades&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subjectId&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mathId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scoreInPercentage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This query looks almost like a series of high-order functions applied to a collection, and, in fact, it is:
everything is strongly-typed and usual stuff (e.g. closing over the &lt;code&gt;mathId&lt;/code&gt; variable) works as expected.&lt;/p&gt;

&lt;p&gt;However, this comes at a price. First of all, &lt;a href=&quot;https://github.com/max-l/Squeryl/tree/master/src/main/scala/org/squeryl/dsl&quot;&gt;the amount of syntactic boilerplate&lt;/a&gt; is truly gigantic.
Moreover, even Scala's type system is still not enough to serve the DSL. In order to capture the &lt;code&gt;subjectId&lt;/code&gt; field access,
Squeryl builds an auxiliary instance of type &lt;code&gt;Grade&lt;/code&gt;, wraps it into a proxy and sifts that proxy through the query during the runtime,
recording accesses to fields and building the corresponding AST.&lt;/p&gt;

&lt;p&gt;And still, all this effort is not enough to provide seamless embedding into the host language. See that &lt;code&gt;===&lt;/code&gt;?
This is a special surrogate operator that stands for &lt;code&gt;==&lt;/code&gt;, because the equality operator itself cannot be overloaded to produce desired results.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;from&lt;/code&gt; were a &lt;code&gt;macro def&lt;/code&gt;, then it would get the &lt;code&gt;g =&gt; ...&lt;/code&gt; function argument
in the form of a &lt;code&gt;Tree&lt;/code&gt; object that represents that query at the compile-time.
As a consequence, one would then be able to use any syntax in the query (including non-overloadable constructs, such as the equality operator).
Also, the lifting (i.e. transformation of domain-specific codes into corresponding domain ASTs) would come for free,
without the need to carefully arrange the web of boilerplate to capture the intent of the user.&lt;/p&gt;

&lt;p&gt;This brings sweet memories of coding up &lt;a href=&quot;https://github.com/xeno-by/conflux&quot;&gt;Conflux&lt;/a&gt;, a framework for programming CUDA in pure C#.
Since the computational architecture of massively parallel devices is incompatible with von Neumann architecture, I didn't even have a luxury of symbolic evaluation
that is employed in Squeryl. Instead I went for writing &lt;a href=&quot;https://github.com/xeno-by/truesight&quot;&gt;a decompiler for .NET bytecode&lt;/a&gt;.
If I were programming in a language with macros, that effort would be completely unnecessary. For example, take a look how a similar goal is achieved by the
&lt;a href=&quot;http://sourceforge.net/projects/nuda/&quot;&gt;NUDA&lt;/a&gt; library written in &lt;a href=&quot;http://nemerle.org&quot;&gt;Nemerle&lt;/a&gt;.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Use case: Language integrated queries</title>
      <link>http://scalamacros.org/usecases/language-integrated-queries.html</link>
      <pubDate>Fri, 11 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/6-language-integrated-queries</guid>
      <description>&lt;p&gt;LINQ is a brilliant concept. Being released five years ago, even now it remains state of the art. However, it has its weaknesses:
1) relying on a clever but rigid compiler hardcode, 2) lack of composability, 3) unclear semantics of calls to external code.&lt;/p&gt;

&lt;p&gt;The trick employed by LINQ is, actually, very neat. Whenever a compiler witnesses an expression of some functional type, say, &lt;code&gt;Func&amp;lt;T, Boolean&amp;gt;&lt;/code&gt;,
in the context that requires an &lt;code&gt;Expression&amp;lt;Func&amp;lt;T, Boolean&amp;gt;&amp;gt;&lt;/code&gt;, it automatically lifts the provided function into an AST that is accessible during the run-time.
This and this alone immediately solves the problem that plagued data access frameworks for a long time (we discuss that problem in detail in the
&lt;a href=&quot;/usecases/advanced-domain-specific-languages.html&quot;&gt;&quot;Advanced domain-specific languages&quot;&lt;/a&gt; use case).&lt;/p&gt;

&lt;p&gt;Being a step forward in comparison with traditional language tools for building eDSLs, LINQ is still not very extensible solution.
For example, LINQ only supports lifting expressions, but not statements, which rules out many useful language constructs, and the programmer cannot do anything about that.&lt;/p&gt;

&lt;p&gt;Also, this particular lifting scheme brings an unfortunate composability restriction. Consider the following code
(the example is taken from the blog post &lt;a href=&quot;http://tomasp.net/blog/linq-expand.aspx&quot;&gt;&quot;Calling functions in LINQ queries&quot;&lt;/a&gt; by Tomas Petricek,
if you want to know more about the composability problem - be sure to take a look at that post):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyPriceFunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Nwind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProductName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StartsWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;B&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Products&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyPriceFunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UnitPrice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code compiles with no errors, but when you execute it DLINQ throws an exception saying: &lt;code&gt;&quot;Static method System.Boolean MyTest(LINQTest.Nwind.Product) has no supported translation to SQL&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This happens because the lifting scheme is hardcoded to be shallow - lifting only processes the expression it sees and does not recursively generate ASTs for the functions used in the query.
There exist several solutions to this problem (one of those is described in the aforementioned blog post), but they are not especially elegant.&lt;/p&gt;

&lt;p&gt;In &lt;a href=&quot;/documentation.html&quot;&gt;our proposal&lt;/a&gt; LINQ can be implemented as follows (below we omit the discussion of LINQ infrastructure, and focus on
the lifting part of the implementation):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Queryable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Repr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Query&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Repr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Impl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Repr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Impl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;c.TypeTag&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Repr:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;c.TypeTag&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;c.Expr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reify&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newBuilder&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reifyTree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Implemented as a &lt;code&gt;macro def&lt;/code&gt;, the &lt;code&gt;filter&lt;/code&gt; high-order function receives its predicate in the form of an AST.
Having done that, the macro is free to perform arbitrary manipulations (e.g. to call the &lt;code&gt;reify&lt;/code&gt; function that builds a domain-specific AST
from the compiler AST that is provided in &lt;code&gt;p&lt;/code&gt;) to accumulate and, eventually, process the query.&lt;/p&gt;

&lt;p&gt;Also, there's a solution to the composability problem: if we declare &lt;code&gt;MyPriceFunc&lt;/code&gt; as a &lt;code&gt;macro def&lt;/code&gt; as well,
then it will expand into the call site and, consequently, will be processed by the domain-specific query translator.
Among alternative approaches is the macro that requests ASTs for all annotated functions in the compilation unit and stores those ASTs for future uses
(that macro could be implemented as a method-level macro annotation or even as a program-wide package annotation).
The domain of possible solutions is quite big, thanks to macros being a core language feature, not an ad-hoc extension to the compiler.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Use case: Type providers</title>
      <link>http://scalamacros.org/usecases/type-providers.html</link>
      <pubDate>Fri, 11 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/5-type-providers</guid>
      <description>&lt;p&gt;&lt;a href=&quot;/media/images/usecases/typical-data-designer.png&quot;&gt;&lt;img src=&quot;/media/images/usecases/typical-data-designer.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code generation is a common solution to the interoperability challenge. It is employed not only for data access, but also for dealing with web services and
in RPC technologies in general (ASN.1, protobuf, to name only a few).&lt;/p&gt;

&lt;p&gt;Despite having traditionally good support from integrated development environments, this approach leaves much to be desired.
Generated code is often unintelligible, is hard to be customized by hand (since the customizations might be lost after regeneration)
and, what's also very important, needs to be fostered by the programmer.
Finally, it's virtually impossible to implement any sufficiently complex code generator without turning it into a spaghetti of textual templates
and control flow that expresses generation logic (the proliferation of various template engines somewhat supports the stated fact).&lt;/p&gt;

&lt;p&gt;Compile-time metaprogramming techniques come to the rescue. By leveraging these techniques, the developer can
programmatically generate required classes into the compiler instead of having to provide them in the source code form.
One of the popular approaches that involves compile-time codegen is represented by F# type providers (take a read of an excellent blog post
&lt;a href=&quot;http://www.mindscapehq.com/blog/index.php/2011/09/19/f-type-providers-as-if-by-magic/&quot;&gt;&quot;F# type providers - as if by magic...&quot;&lt;/a&gt; that outlines
the underlying principles behind type providers):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;[&amp;lt;TypeProvider&amp;gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;VectorProvider&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ITypeProvider&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IProvidedNamespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Cheating for simplicity&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inherit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;[&amp;lt;TypeProvider&amp;gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;VectorProvider&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IProvidedNamespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ResolveTypeName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NamespaceName&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetNestedNamespaces&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We propose an approach of even greater power that leverages the foundations of a macro system to generate the code during the compile-time.
Instead of being a dedicated ad-hoc syntactic construct, macro types are smoothly incorporated into Scala.&lt;/p&gt;

&lt;p&gt;On the one hand, macro types are full-fledged macros, i.e. they can accept parameters in AST form, they can generate resulting ASTs, which provides
necessary flexibility to address code generation tasks.
On the other hand, macro types are interchangeable with regular types: they can be inherited, they can be mixed in, they can participate in instantiation of generic types
and, finally, they can be used as first-class modules (&lt;code&gt;objects&lt;/code&gt; in Scala). The latter is a topic of a separate installation
(see the &lt;a href=&quot;/usecases/generation-of-boilerplate.html&quot;&gt;&quot;Generation of boilerplate&quot;&lt;/a&gt; case study).&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MySqlDb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;connString:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;macro&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MyDb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MySqlDb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Server=127.0.0.1;Database=Foo;&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyDb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startsWith&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toList&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;All in all, macro types solve common problems with code generation by providing flexible extension points into the generation process
and keeping the generator itself readable and maintainable.
This introduces robust solutions to the problems that inevitably require writing boilerplate in modern mainstream languages.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Use case: Generation of boilerplate</title>
      <link>http://scalamacros.org/usecases/generation-of-boilerplate.html</link>
      <pubDate>Fri, 11 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/4-generation-of-boilerplate</guid>
      <description>&lt;p&gt;The following excerpt from &lt;a href=&quot;https://github.com/gkossakowski/virtualization-lms-core/commit/edea90fa0a4be4b9a91d2eb933dd1d08bb6e9a6c&quot;&gt;a development branch&lt;/a&gt;
of LMS project illustrates a typical class of problems.
Sometimes neither object-oriented, nor functional programming can overcome certain kinds of boilerplate:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TupledFunctions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Functions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TupleOps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A1:Manifest&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A2:Manifest&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;B:Manifest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A1&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A1&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tuple2_get1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tuple2_get2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A1:Manifest&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A2:Manifest&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A3:Manifest&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;B:Manifest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A1&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A2&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A1&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A2&lt;/span&gt;,&lt;span class=&quot;kt&quot;&gt;A3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tuple3_get1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tuple3_get2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tuple3_get3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As outlined in the &lt;a href=&quot;/usecases/type-providers.html&quot;&gt;&quot;Type providers&quot;&lt;/a&gt; case study, one of the solutions to the problem is textual code generation.
Often, it's the most obvious and the only accessible one. The aforementioned case study dwells upon the shortcomings of this approach:
1) unintelligibility of generated code, 2) poor extensibility, 3) sub-par maintainability of code generators - and proposes a solution that involves macro types.&lt;/p&gt;

&lt;p&gt;However, there are situations when generating a single type will not save the day. Sometimes the way to go is to create multiple types and/or functions at once.
We considered to introduce macro packages, but decided that macro types will be enough, since Scala &lt;code&gt;objects&lt;/code&gt;
already represent an abstraction equivalent to packages (and even supercede them, because objects can be nested and, as of such, can capture values from the enclosing lexical scope).&lt;/p&gt;

&lt;p&gt;There are plenty of other situations when convenient facilities for boilerplate generation may come in handy.
Just off the top of my head: enums, serialization/deserialization, &lt;a href=&quot;http://www.scala-lang.org/node/9707&quot;&gt;lenses&lt;/a&gt;,
&lt;a href=&quot;http://stackoverflow.com/questions/3900307/cleaner-way-to-update-nested-structures&quot;&gt;zippers&lt;/a&gt; and so on.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Use case: Data types "a la carte"</title>
      <link>http://scalamacros.org/usecases/data-types-a-la-carte.html</link>
      <pubDate>Fri, 11 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/3-data-types-a-la-carte</guid>
      <description>&lt;p&gt;Besides being useful for &lt;a href=&quot;/usecases/type-providers.html&quot;&gt;type providers&lt;/a&gt; and
&lt;a href=&quot;/usecase/generation-of-boilerplate.html&quot;&gt;large-scale code generation&lt;/a&gt;,
macro types provide another interesting opportunity for both a language designer and a programmer.&lt;/p&gt;

&lt;p&gt;Recall that macro types can be mixed in into abritrary classes, and let's think this idea over.
We've already seen how mixing in independently generated logic can be useful for real-world programming tasks,
but what if macro types would generate the code and be aware of their context?&lt;/p&gt;

&lt;p&gt;An augmentation of macro types lets them read the contents (i.e. the members) of the host class.
This is immediately useful for building so called data types &quot;a la carte&quot;. Take case classes. Write the following code
and inspect the output:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;case class Foo(bar: String, baz: Int)&amp;#39;&lt;/span&gt; &amp;gt;&amp;gt; Hello.scala
scalac -Xprint:typer Hello.scala
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We see that the compiler has generated a lot of stuff for a small case class declaration, i.e. among the others
implementations of &lt;code&gt;Product&lt;/code&gt;, &lt;code&gt;Serializable&lt;/code&gt;, &lt;code&gt;equals/hashCode&lt;/code&gt;, &lt;code&gt;toString&lt;/code&gt;, as well as,
the entire companion object. Certain auto-implementations are frequently useful on their own, but, currently, it's all or nothing -
either you declare your class as a &lt;code&gt;case class&lt;/code&gt; and live with all restrictions being imposed, or you don't get any of the
auto-generated goodies.&lt;/p&gt;

&lt;p&gt;A more balanced API can be provided by augmented macro types or, alternatively, macro annotations.
Such macros can discover members of the main class, so they can provide auto-generated functionality in piecemeal fashion.
This restaurant metaphor gives the technique its fancy name.&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Use case: Integration of external DSLs</title>
      <link>http://scalamacros.org/usecases/integration-of-external-dsls.html</link>
      <pubDate>Fri, 11 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/2-integration-of-external-dsls</guid>
      <description>&lt;p&gt;The &lt;a href=&quot;http://www.eecs.harvard.edu/~mainland/publications/mainland07quasiquoting.pdf&quot;&gt;&quot;Why It's Nice to be Quoted: Quasiquoting for Haskell&quot;&lt;/a&gt;
paper by Geoffrey B. Mainland describes an astonishing concept of embedding and integrating arbitrary languages into a sufficiently capable host language.&lt;/p&gt;

&lt;p&gt;One of the most impressive examples in the paper involves a &lt;a href=&quot;http://en.wikipedia.org/wiki/Peephole_optimization&quot;&gt;peephole optimizer&lt;/a&gt; for x86 assembly language.
The example exhibits such prominent features of quasiquoting as: 1) expressing ASTs in native syntax, 2) easy composition, 3) enjoyable pattern matching.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;haskell&quot;&gt;&lt;span class=&quot;nf&quot;&gt;peep&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Asm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Asm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;peep&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r2&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r4&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;je&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lbl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt;
     &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r2&lt;/span&gt;

   &lt;span class=&quot;ow&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r2&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;jmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lbl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The point here is that a language with quasiquotations can subsume and embed arbitrary external domain-specific languages,
granted someone implements quasiquoting services (parsing, splicing, pattern matching) for that language.&lt;/p&gt;

&lt;p&gt;Beauty of this concept from the pragmatic standpoint comes from the fact that these external DSLs
can be processed (for example, validated) by macros, which are evaluated during the compile time.
This means that format strings, regexes, XML (less relevant for Scala), HTML, JavaScript - all those can be to certain extent validated during the compilation of the application,
bringing additional static safety to the program.&lt;/p&gt;

&lt;p&gt;With the syntax introduced by (an unrelated) &lt;a href=&quot;https://docs.google.com/document/d/1NdxNxZYodPA-c4MLr33KzwzKFkzm9iW9POexT9PkJsU/edit?hl=en_US&amp;pli=1&quot;&gt;SIP: String interpolation and formatting&lt;/a&gt;,
it becomes possible to write quasiquotations in Scala, and with macros it becomes possible to process these quasiquotations during the compile-time.&lt;/p&gt;

&lt;p&gt;Another useful application of this notion is convenient notation for new kinds of literals.
For example, using the syntax from the aforementioned string interpolation SIP one could write &lt;code&gt;b&quot;10001&quot;&lt;/code&gt; to express an integer literal &lt;code&gt;17&lt;/code&gt;.
The interpolation would be processed by a macro during the compile-time, which means that the programmer wouldn't pay any runtime penalty for the convenience.&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Use case: Units of measure</title>
      <link>http://scalamacros.org/usecases/units-of-measure.html</link>
      <pubDate>Fri, 11 Nov 2011 00:00:00 PST</pubDate>
      <author>dev@scalamacros.org</author>
      <guid>http://scalamacros.org/usecases/1-units-of-measure</guid>
      <description>&lt;p&gt;One of the interesting requests in the comments to &lt;a href=&quot;http://xeno-by.blogspot.com/2011/10/scala-macros-status-update-and-call-for.html&quot;&gt;my recent post&lt;/a&gt;
was units of measure for Scala. Units of measure represent an augmentation of the type system of F# (you might wish up to read a blog series for more details:
&lt;a href=&quot;http://blogs.msdn.com/b/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-units.aspx&quot;&gt;first post&lt;/a&gt;,
&lt;a href=&quot;http://blogs.msdn.com/b/andrewkennedy/archive/2008/09/02/units-of-measure-in-f-part-two-unit-conversions.aspx&quot;&gt;second post&lt;/a&gt;,
&lt;a href=&quot;http://blogs.msdn.com/b/andrewkennedy/archive/2008/09/04/units-of-measure-in-f-part-three-generic-units.aspx&quot;&gt;third post&lt;/a&gt;,
&lt;a href=&quot;http://blogs.msdn.com/b/andrewkennedy/archive/2009/06/09/units-of-measure-in-f_2300_-part-four-parameterized-types.aspx&quot;&gt;final post&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Despite the fact that compile-time metaprogramming lies on a different (lower!) level than type systems, this very use case can be implemented with macros.
This brings an interesting question of when macros are appropriate, and when it's better to do away with them in favor of other language mechanisms.
We will elaborate on this topic in future installations.&lt;/p&gt;

&lt;p&gt;In the following example F# compiler is capable of inferring the unit of measure for a derived value &lt;code&gt;speedOfImpact&lt;/code&gt;
(the dimensionality will be &lt;code&gt;m/s&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ocaml&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gravityOnEarth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;81&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heightOfMyOfficeWindow&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;speedOfImpact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gravityOnEarth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heightOfMyOfficeWindow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With macro types it becomes possible to write the code like that:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gravityOnEarth&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;9.81&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;m/s^2&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heightOfMyOfficeWindow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;m&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;speedOfImpact&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gravityOnEarth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heightOfMyOfficeWindow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/code&gt;u&lt;/code&gt; is a &lt;code&gt;macro def&lt;/code&gt; that takes a number and wraps it in a macro type that depends on the unit of measure passed.
That macro type hosts all necessary stuff that overloads relevant operations (&lt;code&gt;arithmetic&lt;/code&gt;, &lt;code&gt;math._&lt;/code&gt; stuff, so on).
These operations upon units of measure would themselves be macros that produce new types as necessary.&lt;/p&gt;

&lt;p&gt;An alternative approach would be to implement &lt;code&gt;u&lt;/code&gt; as a macro annotation that would operate on expression-level
and ascribe numeric values with units of measure. Such a macro would be able to do everything a macro def can do, but it would look in, arguably, a nicer way:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;
&lt;pre&gt;&lt;code class=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gravityOnEarth&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;9.81&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;m/s^2&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heightOfMyOfficeWindow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;3.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;m&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;speedOfImpact&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gravityOnEarth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heightOfMyOfficeWindow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;At a glance, this looks like a workable solution, but, of course, it will be possible to fully assess the feasibility of this idea
only when we have an implementation of the macro system that can be used for tests.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>