DaAnalyst
Hi,
Is there any way to specify a map or a structure extension using typespecs, e.g.:
@type map1() :: %{
field1: String.t(),
field2: atom()
}
@type map2() :: %{
map1() |
field3: integer(),
field4: integer()
}
Naturally, the above wouldn’t compile, but is there any way to achieve the same effect? If not, how about adding this feature to typespecs?
TIA
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Not really the answer You are looking for, but maybe typecheck can help…
Qqwy
In Elixir’s typespec syntax itself, this is not possible. There is no notion in Elixir/Erlang’s typespecs of ‘a couple of keys’ that you add to the specification of another map.
You have the choice of adding one or multiple
optional(key_type) => value_type()at the end (wherekey_typemight be a single atom), but a key-value pair like this (e.g.optional(key_type) => value_type()) does not have a type on its own and cannot be embedded on its own.Since Elixir’s typespecs have feature-parity with Erlang’s typespecs (there is a little bit of extra syntactic sugar but they are 100% compatible) I do not think a feature like this will ever be added.
As @kokolegorille already pointed out, there are ways to add (runtime) type-checks to your code that might be able to do checks like this.
), but it is fully possible to build it yourself in user code and integrate it with the rest of TypeCheck.
Currently in the TypeCheck library the notion of a key-value pair also does not exist as its own structure (it is an interesting feature though, PR’s are very welcome
Qqwy
The best you can do in Elixir itself, is something crazy like this (I do not recommend doing this!):
So we are able to inject type snippets into other types using quote/unquote.
However, since most types are not valid as values, we also need to have the extra
quotearound the list. Also, the parentheses around this quote are not optional, because of a parsing precedence edge case in Elixir, making it even more clear that we’re in unexplored waters here: You’re not intended to write this kind of code.So: This seems to me a clear situation in which the ‘cure’ is worse than the ‘disease’.
DaAnalyst
The cure indeed looks worse than the disease, but thank you for going into length with this.
Erlang feature-parity be damned, I still think such a basic tool for reusing base structure fields elsewhere should be made available. For instance, my “defextends” macro helps me “extend” elixir structures without needing to copy the “inherited” fields and it proved to be very useful, but I still lack a tool for writing the typespecs.
As for TypeCheck, I don’t think it addresses this problem at all.
Thanks
Qqwy
In that case I might be misunderstanding your use case. I thought your scenario was that you want to extend the typespec of one struct with some of the fields of the typespec of another struct. TypeCheck is able to facilitate that, and create the final ‘Elixir-compatible’ typespecs to be shown in the documentation and e.g. passed to Dialyzer for you.
.
But if I’m not correctly paraphrasing your scenario, please do explain
DaAnalyst
I took a quick look at the TypeCheck docs and I couldn’t find any mention of extending/inheriting from a structure or a map. Can you please provide a snippet of how it may be done using TypeCheck?
Thanks
Qqwy
Thank you for this question! I attempted to do it, but I did encounter a problem.
In essence, creating functions that take TypeCheck type-structs as input and generate other type-structs as output is ‘easy’:
and indeed, if we were to add this to the
TypeCheck.Builtinmodule, it would work as intended, and you could e.g. write:However, currently there is no easy way for a user to make their own custom “type-level” functions available for usage in the types, as type specifications are evaluated in a slightly special context because of implementation reasons.
I’ve created an issue on the TypeCheck repository to tackle this.
So to answer your question: No, it is currently not possible, but will be "soon
".
DaAnalyst
Thanks! Will be glad to check it out when you add the feature. Please, make the use as neat as possible.
Another thing. As you can imagine, the requirement for a map/struct extension often regards inter-module references e.g.: