tangui

tangui

Getting the @type module attribute

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

Marked As Solved

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.

Also Liked

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

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:

Last Post!

josevalim

josevalim

Creator of Elixir

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

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement