tangui

tangui

Hi all,

I’ve been playing with Specify lately and I’m trying to get the @type module attribute like the @doc one, so as to link to that type in my documentation.

Configuration looks like this:

  Specify.defconfig do
    @doc "there are no floors for me to sweep"

    @type floors_to_sweep :: non_neg_integer()

    field :floors_to_sweep, default: 0
  end

Trying to get the @type module attribute always results in a nil value:

        field_documentation = Module.delete_attribute(__MODULE__, :doc)
        IO.inspect(Module.delete_attribute(__MODULE__, :type))

The :doc is retrieved, but not the :type module attribute.

I suspect the @type is processed in a particular way. Is there any way to retrieve it?

Cheers

Showing Posts 1 to 9

josevalim

josevalim

Creator of Elixir

Yeah, you can’t get @type, @spec, @callback and friends because they have special semantics. Usually, what is stored in a module attribute is evaluated, if you do @foo 1 + 1, the value of @foo is the result of 1 + 1. For typespecs, we actually receive an AST that we process, and returning that would leak some implementation details.

Qqwy

Qqwy

TypeCheck Core Team

Interesting use case!

Currently, when you add @type-annotations inside the defconfig, they will end up being shown in the documentation just as if they were written outside of the defconfig.
So t YourModule.floors_to_sweep would work in IEx, and in the compiled HTML documentation, the type would also be listed.

If you want to fully link to a type, what about writing a parser function and adding a @spec to that one? The field documentation will link to its parser function’s documentation, and in that way you can keep the semantics together. (The other advantage is that you can re-use the same type/parser for multiple fields).

tangui

tangui OP

Thanks for your answers.

@Qqwy: not sure to understand and if it’s the same use case :slight_smile: . Mine is mainly about documenting hundreds (if not more) configuration options including many callbacks with most of the time different signatures, and linking between them (config option ↔ type), and also to help dialyzer with detecting type errors. Nice lib by the way!

Qqwy

Qqwy

TypeCheck Core Team

I think this part is already covered by the existing functionality. Every module that uses defconfig will have, for every field in the documentation, the following part in their docs:

Example here.

This is indeed not something that Specify currently helps you with. It definitely would be interesting to automate the generation of a ‘config typespec’ in some way. Maybe the field types can be inferred from the parser function’s spec, and we can then define a type for the config struct as a whole. :thinking:

Thanks! :blush:

tangui

tangui OP

I’ve tried it and it’s somehow difficult to generate some specs because of the special syntaxes of @type and @spec (::, etc.). See also: Dynamically generate typespecs from module attribute list

Qqwy

Qqwy

TypeCheck Core Team

I’m fairly certain this is what Code.Typespec is meant to be used for.
We could (as long as the parser function is readable as an invocation of a certain MFA) call Code.Typespec.fetch_specs(module), find the appropriate function’s spec in the response, and then use Code.Typespec.spec_to_quoted to turn it back into Elixir AST, which, assuming that it has the format some_fun(maybe, some, arguments) :: {:ok, result_type} | {:error, problem} means that we should be able to extract the result_type from here.
The one thing I haven’t figured out yet is how to disambugate remote types from local and built-in ones.

tangui

tangui OP

I’m now trying to get the @moduledoc module attribute in a __using__ macro but in:

      {line_number, existing_moduledoc} =
        Module.delete_attribute(__MODULE__, :moduledoc) || {0, ""}

      Module.put_attribute(
        __MODULE__,
        :moduledoc,
        {line_number, existing_moduledoc <> additional_doc})

Module.delete_attribute/2 always returns nil (although the @moduledoc is set) and Module.put_attribute/3 doesn’t set it (nothing in generated doc).

Is there an exception with @moduledoc? I suspect it is not set yet when calling Module.delete_attribute/2. Also Module.put_attribute/3is later erased by the plain @moduledoc (I’ve tested it - no @moduledoc in the target module results in additional_doc being displayed).

jackalcooper

jackalcooper

Is this still true today? Or there are ways to get the type specs for functions now.

josevalim

josevalim

Creator of Elixir

Afaik there is still no public API for doing so. :frowning:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews