Scala 3
Published in

Scala 3

Scala 3: Macros

Six Vista Towers, Chicago (from Navy Pier), © 2021, Dean Wampler

Last time I introduced one of the new metaprogramming features in Scala 3, the inline keyword and how it affects source code. The example I used also contained a macro definition, using quoting and splicing. This post introduces those concepts.

Programming Scala, Third Edition is now available. It provides a comprehensive introduction to Scala 3 changes for experienced Scala developers, as well as a complete introduction to Scala for new Scala developers.

Here is the previous example, which ensures that an invariant holds before and after a block of code is executed.

Quotation and splicing are the key components of compile-time macros in Scala 3, which is a new system replacing the experimental system in Scala 2.

Quotation is where sections of source code are converted to a tree-like data structure of type Expr[T], where T corresponds to the expression’s type. A quotation of an expression uses the syntax '{...}, with the source code to quote inside the braces. For types, '[...] is used.

Splicing goes the other direction, converting quotations back to source code. Splices are expressed using ${...}, which deliberate draws a parallel with string interpolation.

Both are used together with inline to construct code programmatically at compile time. There is also a runtime staging capability, which uses quotes and splices, but not inline, to construct code at runtime instead of compile time. I’ll explore runtime staging in a subsequent post.

Last time I explained what the InvariantEnabled.apply method is doing and how inline determines what byte code actually gets generated, depending on compile-time analysis of the ignore field, etc.

If the predicate evaluates to false, the private method fail is called, which uses a splice ${...} to construct the correct source code from the Expr[String] returned by failImpl. An Expr[T] encapsulates the “tree-like data structure” mentioned above.

When fail has finished its work, new source code will have been inserted into the compilation stream for the compiler to compile. Using inline allows quoting and splicing to happen at compile time.

Within a quote or splice, identifiers can be quoted or spliced, such as the arguments ‘predicate, ‘message, ‘block, ‘beforeAfter that are passed to failImpl. Note the types of the arguments received by failImpl. Each is an Expr[T] for some T. So, for example, writing ‘predicate converts the by-name Boolean predicate into an Expr[Boolean]. There is also a using scala.quotes.Quotes instance required by failImpl.

Notice what happens in the body of failImpl. Since we are returning an Expr[String], the body consists of ‘{...}. Inside the braces is code to construct and throw the InvariantFailure exception.

Most of work is in the construction of the message string. In ${showExpr(predicate)}, the helper method showExpr effectively returns the string representation for the input Expr[Boolean] as an Expr[String]. What this means practically is the source code for the expression is captured as a string. The returned Expr[String] is then spliced into the larger message string with the outer set of ${}. The other input parameters are evaluated similarly, except for beforeAndAfter, which doesn’t need to be passed through showExpr, because it is already an Expr[String].

Recall from the previous post that the point of all this is to create an error message string with the actual source code that failed the invariant check. We also saw in the example we used that the code had already been converted from operator notation when the string representation was generated by the macro. For example, thing1.label != “label" became thing1.label.!=(“label”) and i * 2 % 3 became i.*(2).%(3). Not perfect, but good enough for debugging failures.

For an overview of Scala 3 metaprogramming, see this Dotty documentation, which also links to more detailed information about macros.

See Programming Scala, Third Edition for more information about the new metaprogramming facilities and Scala 3, in general.

--

--

--

A series of posts on Scala version 3, what’s new and why, and how to use its new features effectively. For more details, visit http://programming-scala.org/.

Recommended from Medium

Implementing UUID for Model User in Django & PostgreSQL

Managing data science code with Git and GitLab

The Computer in your Computer — The Intel Management Engine

Microservices maturity model: From Monolith to Kubernetes

Six Steps to Sending Data to Any Destination Using RudderStack

mmap in Go considered harmful

The Waterfall Method For Project Management

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dean Wampler

Dean Wampler

The person who’s wrong on the Internet. ML/AI and FP enthusiast. Accelerated Discovery at IBM Research. Speaker, author, aspiring photographer.

More from Medium

Safer Exceptions in Scala 3

Why use Cats IO instead of Scala Futures?

Scala: Partial functions andThen

Understanding the “Found ZIO.Task Required ZIO.ZIO” dilemma