marcandre

marcandre

Seeking clarification of dependencies of structs in definitions / typespecs

I fear I am missing a few things, but after thinking about the dependencies that structs generate, both in typespecs and in function definition, it looks to me like:

  • structs in function definitions should introduce a runtime dependency instead of an export dependency
  • structs in typespecs should not introduce an export nor a runtime dependency.
  • Example.t() in typespecs should have similar effects as %Example{}

Structs in method definitions

# a.ex
defmodule A do
  @spec foo(%B{}) :: %C{}
  def foo(%B{example: x}) do
    %C{example: x * 2}
  end
end

As far as I understand, compiling the def above currently requires knowing B and C to insure that example is a field, and for no other reason. Am I missing something?

If I am not, why would the check that example is a field not be done at build time, same as when we check that SomeModule.foo() exists?

Structs in typespecs

The @spec above introduces an export dependency on B & C.

I presume this is for the same reason, i.e. if we were to write additional spec for the fields (we basically never in practice though), then this would generate an error when compiling if keys are invalid.

Again, am I missing some other reason?

If not, then that check could also be done at build time instead, or not at all (like Example.t() in typespecs)

It seems clear to me that even a runtime dependency would actually be too strict, because it could still introduce useless transitive compile time dependencies. These would always be “false alarms”, i.e,. would trigger a recompilation that wouldn’t change the output.

This is rarely important for typespecs of functions because ultimately the implementation will typically have runtime dependencies on these modules, but for field declarations or @callback, this can make a big difference.

Type references in typespecs

I don’t know why there is no checking of the validity of types used like Example.t(). Maybe the idea is to rely solely on Dialyxir?

I am surprised that changing Example.t() to %Example{} in a typespec would be so different as it is current. It seems clear to me that both forms should have the same effect on the dependency graph (i.e. none) and checks (i.e. either a build-time check for validity, or none and we rely on dialyxir).

At first I tought that it might be useful to introduce a lower type of dependency (say “buildtime dependency”) for %Example{} and Example.t(), that would not count for transitive dependencies, but I can’t see the point of exposing it and might just be more confusing than anything.

Reducing export dependencies to either runtime dependencies, or to none at all seem like a clear gain. What am I missing?

Most Liked

josevalim

josevalim

Creator of Elixir

The answer is in what the code expands to. %B{} in typespecs has to expand to %{__struct__: B, field1: term(), field2: term(), example: ...}, etc. Therefore, adding or removing any struct field (which reflects on the exported definition of B), requires recompilation. The same happens for %B{} in a function body, we need to expand all fields.

In both cases, inside a function or inside a spec, B.t() addresses this because it encapsulates these concerns into a function, there is no code expansion.

The only exception is %B{example: ...} inside a pattern. The code expands to is %{__struct__: B, example: ...}. So in theory it could be a runtime dependency and a later pass could warn the struct is no longer defined or the field does not exist. But I am not quite sure it is worth special casing around this.

josevalim

josevalim

Creator of Elixir

Correct and correct.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
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
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement