Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non-reflective abstractions for Java codegen Contracts #13724

Merged
merged 29 commits into from
May 6, 2022

Conversation

S11001001
Copy link
Contributor

@S11001001 S11001001 commented Apr 27, 2022

Fixes #13471. readActiveContractKeys and readActiveContractPayloads are simple examples of functions that would not have been possible to write before.

  • well-typed parents for generated ContractId and Contract
  • move a bunch of things to these classes instead of code-generating them
  • static COMPANION as a now-and-future home for well-typed abstractions over things that are currently just static methods

Eta-expanding static methods into functions works for many use cases. But it doesn't really let us build stuff on top of those methods once, without "infectious codegen". An example is the new fromCreatedEvent: this method is now hand-written, with only static forwarding shims codegenned.

Other things can usefully be moved into ContractCompanion and its signature and hierarchy usefully expanded. fromValue is a really good candidate. But this PR is doing quite enough.

CHANGELOG_BEGIN
- [daml codegen js] Generated templates have types, inherited methods,
  and a new static ``COMPANION`` that enable writing more
  template-generic utilities directly in Java.  New classes
  ``Contract``, ``ContractWithKey``, and ``ContractCompanion``, have
  been introduced; if you are importing
  ``com.daml.ledger.javaapi.data.codegen.*`` and have imported classes
  with these names from any other package, these references will stop
  working; use individual imports instead.
  See `issue #13724 <https://github.com/digital-asset/daml/pull/13724>`__.
CHANGELOG_END
  • keyed contracts
  • companion
  • eliminate generated contract toString?
  • test abstract usage
  • changelog
  • remove abstract deprecated method, not needed for static forwarders
  • make Contract vs ContractWithKey more like ContractCompanion.WithoutKey vs ContractCompanion.WithKey?
  • call ContractCompanion TemplateCompanion instead, as with Scala codegen?

@S11001001 S11001001 added component/java-ecosystem Java development experience team/ledger-clients Related to the Ledger Clients team's components. labels Apr 27, 2022
@S11001001 S11001001 self-assigned this Apr 27, 2022
CHANGELOG_BEGIN
- [daml codegen js] Generated templates have types, inherited methods,
  and a new static ``COMPANION`` that enable writing more
  template-generic utilities directly in Java.
  See `issue #13724 <https://github.com/digital-asset/daml/pull/13724>`__.
CHANGELOG_END
@S11001001 S11001001 marked this pull request as ready for review April 29, 2022 21:20
@S11001001 S11001001 requested a review from realvictorprm April 29, 2022 21:20
Copy link
Contributor

@stefanobaghino-da stefanobaghino-da left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a blocker to make sure a few important points don't go unaddressed:

  • If a client changes a Daml model, upgrades to a new version where this PR got merged and re-generates code purely with the intent of generating code for the changed Daml models, will existing client code cease to compile, raise compilation warnings or change runtime behavior?
  • How does this feature interact with the decoder class? Is there a change in approach enabled by this change?
  • This adds a new feature and/or changes the way in which an existing feature can be changed: is this sufficiently documented to enable users to start using this independently after going through the documentation?

@S11001001
Copy link
Contributor Author

If a client changes a Daml model, upgrades to a new version where this PR got merged and re-generates code purely with the intent of generating code for the changed Daml models, will existing client code cease to compile,

@stefanobaghino-da Their code may fail javac if they import com.daml.ledger.javaapi.data.codegen.* and also com.daml.ledger.javaapi.data.Contract (wildcard or no), and refer to Contract. No warnings or behavior changes.

raise compilation warnings or change runtime behavior?

No.

How does this feature interact with the decoder class?

Not in this PR.

Is there a change in approach enabled by this change?

Yes, discussed in #13768.

This adds a new feature and/or changes the way in which an existing feature can be changed: is this sufficiently documented to enable users to start using this independently after going through the documentation?

A user who is motivated to build the kinds of template-generic functions this PR enables should find all they need to know in the type signatures, with the understanding that they are just like the static methods of the same signature.

If we were happy with the documentation of fromCreatedEvent before, then this feature offers exactly the same experience. If we want to expand on that, the Javadoc is the best place to do so; I do not think docs.daml.com should cover this in detail other than to mention that it is generated, as the example here does.

@stefanobaghino-da
Copy link
Contributor

@stefanobaghino-da Their code may fail javac if they import com.daml.ledger.javaapi.data.codegen.* and also com.daml.ledger.javaapi.data.Contract (wildcard or no), and refer to Contract. No warnings or behavior changes.

Please add this consideration to the change log so that the release coordinator can be aware of this. It's very important that we minimize the impact of changes and give a clear explanation on how to migrate if there is an impact. I'll remove my blocker in light of your replies. Thanks.

@stefanobaghino-da stefanobaghino-da dismissed their stale review May 6, 2022 16:27

Concerns addressed in #13724 (comment)

S11001001 added 3 commits May 6, 2022 13:49
CHANGELOG_BEGIN
- [daml codegen js] Generated templates have types, inherited methods,
  and a new static ``COMPANION`` that enable writing more
  template-generic utilities directly in Java.  New classes
  ``Contract``, ``ContractWithKey``, and ``ContractCompanion``, have
  been introduced; if you are importing
  ``com.daml.ledger.javaapi.data.codegen.*`` and have imported classes
  with these names from any other package, these references will stop
  working; use individual imports instead.
  See `issue #13724 <https://github.com/digital-asset/daml/pull/13724>`__.
CHANGELOG_END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/java-ecosystem Java development experience team/ledger-clients Related to the Ledger Clients team's components.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Make Java codegen more OO friendly by adding contract methods to empty contract Interface
3 participants