daml/language-support/scala/codegen
Samir Talwar 4b8b67a1b5
Upgrade Scalatest to v3.2.9. (#10576)
* Upgrade Scalatest to v3.2.9.

Because of some coupling we also have to upgrade Scalaz to the latest
v7.2 point release, v7.2.33.

The Scalatest changes are quite involved because the JAR has been broken
up into several smaller JARs. Because Bazel expects us to specify all
dependencies and doesn't allow transitive dependencies to be used
directly, this means that we need to specify the explicit Scalatest
components that we use.

As you can imagine, this results in quite a big set of changes. They
are, however, constrained to dependency management; all the code remains
the same.

CHANGELOG_BEGIN
CHANGELOG_END

* http-json-oracle: Fix a Scalatest dependency.

* ledger-api-client: Fix a Scalatest dependency.
2021-08-12 23:19:35 +00:00
..
src LF: change type from Try to Either in archive module (#10277) 2021-07-14 19:24:31 +00:00
BUILD.bazel Upgrade Scalatest to v3.2.9. (#10576) 2021-08-12 23:19:35 +00:00
codegen.bzl language-support/scala/codegen: Set the logging level to WARN. (#10165) 2021-07-01 16:49:46 +00:00
README.md Daml case and logo (#8433) 2021-01-08 12:50:15 +00:00

Developer's guide to Daml Scala code generator

For the User's guide to the Scala code generator see: docs/daml-scala-code-gen-user-guide.rst

Introduction

The Daml Scala code generator produces wrapper classes that correspond to Daml contract templates and Daml user defined types (records/variants that are the right hand side of a Daml type synonym). They are intended to be used by application developers and they provide the same level of type safety as the Daml langauge itself.

Working with Scala macros

The code makes heavy use of Scala macros and quasiquotes.

More information on Scala macros can be found here

There is some great documentation on quasiquotes here In particular, this page is a great reference

You may also find this guide on the Scala Reflection API useful as macros/quasiquotes make use of it. See here

The reference documentation for the Reflection API is available here

I have also found it very useful to use the Scala console to see the abstract syntax tree (AST) structure

showRaw is your friend

You can use the Reflection API's showRaw function to see which constructors are used in a quasiquoted expression/statement/definition.

From an SBT console open a Scala console with:

> console

Then:

scala> val universe: scala.reflect.runtime.universe.type = scala.reflect.runtime.universe
universe: reflect.runtime.universe.type = scala.reflect.runtime.JavaUniverse@712e80d8

scala> import universe._
import universe._

Then (for example):

scala> showRaw(q"obj.method")
res2: String = Select(Ident(TermName("obj")), TermName("method"))

Use Ctrl-D to get back to SBT console

Understanding the code

The best way to understand the structure of the generated code is to read DamlContractTemplate.scala and DamlUserDefinedType.scala. The Scala macros are quite readable and both classes have informative comments.