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

Annotations for extension API #29

Merged
merged 6 commits into from
Dec 21, 2024
Merged

Annotations for extension API #29

merged 6 commits into from
Dec 21, 2024

Conversation

arnodirlam
Copy link
Contributor

@arnodirlam arnodirlam commented Nov 21, 2024

This changes the API for making existing modules (e.g. the standard library) compatible with Dx.

All functions that take functions as arguments must be implemented natively, i.e. not via defd but via explicit handling of Dx-internal return types, usually {:ok, ...} or {:not_loaded}.

To aid with the implementation, the Dx compiler provides an API to pre-process the arguments before they're passed to the functions, e.g. loading any scopes (used to lazily translate Elixir code to SQL). See the extension API docs for more info. The compiler can skip any unneeded functions, and perform other optimizations.

This API is currently defined via a callback function, __fun_info(fun_name, arity).

This PR introduces annotations that are co-located with the function definitions, instead of defining all callback clauses at the top of the module, and implementing the functions further below. See the changed in Dx.Enum in this PR for an example.

To pick up the annotations, def must be changed to defd_, standing for "native defd". The only difference currently being that it supports the annotations.

Further options

These options are non-exclusive:

Allow annotations for def (removing defd_)

This would make it more intuitive, as it's in line with libraries such as decorator.

On the other hand, keeping defd_ could be used to define modules with a mix of defd, defd_ and def (not compatible with defd) functions.

Rename the callback & annotations to __dx_compile__ and @dx_compile

This would make it clearer that it's an interface for instructions to the Dx compiler.

@arnodirlam
Copy link
Contributor Author

Now going all-in with the _ suffix naming. Better to have something consistent for now than anything else.

defmodule Dx.Enum do
  use Dx.Defd_

  @dx_ args: [[:atom_to_scope, :preload_scope], :final_args_fn]
  defd_ map(enum, fun) do
    # ...
  end
end

@arnodirlam arnodirlam force-pushed the extension-api-annotations branch from 6177cbe to 7619dcd Compare December 9, 2024 18:05
@arnodirlam arnodirlam force-pushed the extension-api-annotations branch from 7619dcd to bb7515e Compare December 9, 2024 18:05
@arnodirlam arnodirlam marked this pull request as ready for review December 21, 2024 11:50
@arnodirlam arnodirlam merged commit d9b2bf8 into main Dec 21, 2024
9 checks passed
@arnodirlam arnodirlam deleted the extension-api-annotations branch January 5, 2025 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant