Is there a way to get the types and specs that are defined in a module in some machine-readable format? I have never seen docs on that
You can use:
Code.Typespec.fetch_specs(Map)
The format is quite raw though. You could also use ExDoc to pull these out:
[doc] = ExDoc.Retriever.docs_from_modules([Map], %ExDoc.Config{})
iex(5)> doc.typespecs
[
%ExDoc.TypeNode{
annotations: [],
arity: 0,
deprecated: nil,
doc: nil,
doc_line: 96,
id: "key/0",
name: :key,
signature: "key()",
source_path: "/home/ubuntu/bob/tmp/412672862f47582cfc8e244ce57c682a/elixir/lib/elixir/lib/map.ex",
source_url: nil,
spec: {:::, [], [{:key, [], []}, {:any, [line: 96], []}]},
type: :type
},
%ExDoc.TypeNode{
annotations: [],
arity: 0,
deprecated: nil,
doc: nil,
doc_line: 97,
id: "value/0",
name: :value,
signature: "value()",
source_path: "/home/ubuntu/bob/tmp/412672862f47582cfc8e244ce57c682a/elixir/lib/elixir/lib/map.ex",
source_url: nil,
spec: {:::, [], [{:value, [], []}, {:any, [line: 97], []}]},
type: :type
}
]
Although both of these methods are relying on private apis so are not ideal. I found these by scanning through the ExDoc.Retriever
module
2 Likes