daml/language-support/scala/codegen
Andreas Herrmann f33e79c787
Remove unused dependencies to da_scala_library (#3938)
* Inline all scala_library dependencies

* Run //:buildifier-fix

* TMP scala_library_suite --> scala_library

* da_scala_library: Enable unused dependency checker

* scala_library: Enable unused dependency checker

* //daml-lf/data:data

* //daml-lf/engine:engine

* //ledger-api/rs-grpc-akka:rs-grpc-akka

* //ledger/participant-state:participant-state

* //ledger/ledger-api-client:ledger-api-client

* //scala-protoc-plugins/scala-logging:scala-logging-lib

* //ledger/ledger-api-scala-logging:ledger-api-scala-logging

* //ledger/ledger-api-common:ledger-api-common

* //ledger-service/utils:utils

* //ledger-service/jwt:jwt

* //ledger/ledger-api-auth:ledger-api-auth

* //extractor:extractor

* //daml-assistant/scala-daml-project-config:scala-daml-project-config

* //language-support/codegen-common:codegen-common

* //language-support/scala/codegen:codegen

* //language-support/codegen-main:codegen-main-lib

* //ledger-service/db-backend:db-backend

* //ledger-service/http-json:http-json

* //daml-lf/scenario-interpreter:scenario-interpreter

* //ledger/sandbox:sandbox

* //navigator/backend:navigator-library

* //daml-assistant/daml-sdk:sdk-lib

* //daml-lf/data-scalacheck:data-scalacheck

* //daml-script/test:test-lib

* //ledger/ledger-api-common:ledger-api-common-scala-tests-lib

* //ledger/test-common:test-common

* //ledger/sandbox:sandbox-scala-tests-lib

* //extractor:extractor-scala-tests-lib

* //language-support/java/bindings:bindings-java-tests-lib

* //language-support/java/bindings-rxjava:bindings-java-tests-lib

* //language-support/scala/bindings-akka-testing:bindings-akka-testing

* //language-support/scala/codegen-testing:codegen-testing

* //language-support/scala/codegen-sample-app:daml-lf-codegen-sample-app

* //language-support/scala/codegen-sample-app:daml-lf-codegen-sample-app-testing

* //language-support/scala/codegen-testing:codegen-testing-testing

* //ledger-api/sample-service:sample-service

* //ledger-api/rs-grpc-akka:rs-grpc-akka-tests-lib

* //ledger/ledger-api-test-tool:ledger-api-test-tool-lib

* //ledger/ledger-api-test-tool:ledger-api-test-tool-tests

* //ledger/participant-state/kvutils:kvutils

* //ledger/sandbox:ledger-api-server

* //ledger/sandbox-perf:sandbox-perf-lib

* //navigator/backend:navigator-tests-library

* UNDO scala_library_suite --> scala_library

This reverts commit ab3eb1ae23139e2ec431ab4551fbb0371e0354e1.

Co-authored-by: Andreas Herrmann <andreash87@gmx.ch>
2020-01-06 18:14:21 +01:00
..
docs update copyright notices to 2020 (#3939) 2020-01-02 21:21:13 +01:00
src update copyright notices to 2020 (#3939) 2020-01-02 21:21:13 +01:00
BUILD.bazel Remove unused dependencies to da_scala_library (#3938) 2020-01-06 18:14:21 +01:00
codegen.bzl update copyright notices to 2020 (#3939) 2020-01-02 21:21:13 +01:00
README.md open-sourcing daml 2019-04-04 09:33:38 +01: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.