akin

akin

Need help understanding the Spark library

Questions abut Spark

I am working on an Elixir library that provides a Domain-Specific Language (DSL). I plan to use the Spark library and have a few questions regarding its implementation in a project. My questions are as follows:

  1. With Spark, how do you implement the actual functionality of your DSL? Specifically, once you have defined your sections and entities, how do you define the code that will be quoted by each section and entity?
  2. Am I correct in assuming that the sections and entities are macros that quote code, or is there something fundamental about Spark’s operation that I’m missing?
  3. My library focuses more on using a single macro with numerous options rather than nesting multiple macros. I’m primarily interested in Spark for its tooling capabilities. Given this context, do you think Spark would be a good fit for my library?

Any help is welcome.

Most Liked

zachdaniel

zachdaniel

Creator of Ash

Hello! So spark is primarily designed for helping to build “structural” DSLs. I.e defining data.

For example, in Ash Framework, on an Ash.Resource

attributes do
  attribute :name, :string, allow_nil?: false
end

That is a section called :attributes, with an entity called :attribute. We then extract this using:

Ash.Resource.Info.attributes(module, :attributes)

which gives us, for example:

%Attribute{
  name: :name,
  type: :string,
  allow_nil?: false
}

(the examples are greatly simplified from real life).

So the DSL simply holds the description. It’s just “something else” (whatever else you want) that contains the “actual functionality” of the DSL. You can leverage it at compile time to do code generation, or at runtime to interpret the data structure however you like.

If you want things to happen “in the module”, then you can use transformers, i.e

defmodule YourTransformer do
  use Spark.Dsl.Transformer

  def transform(dsl_state) do
    {:ok, 
      Spark.Dsl.Transformer.eval(dsl_state, [], quote do
         ...your quoted code here
      end)
    }
  end
end

If you’re looking to write something like a single macro that contains some kind of customized imperative/declarative/expression language, then spark is unlikely to help you.

i.e if you’re looking to make something like NX or something like Ecto’s Ecto.Query macros.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement