gmile

gmile

As part of Understanding a spike in app compilation time, I’m trying to remove compile-time dependency that’s based on defguard macro.

With the code below, Elixir reports a transitive compile-time dependency:

mix xref graph --label compile-connected
lib/example1.ex
└── lib/guards.ex (compile)

How would one go about change the code to avoid the transitive dependency, while (ideally) preserving the defguard macros?

For context, this is a simplified example of production code. The reason lib/guards.ex is a separate file is code re-use - it’s typically included into other modules to offer the guards.

The code looks like this (repo is available here, for reference):

# lib/example1.ex
defmodule Example1 do
  import Example1.Guards

  def user(user) when is_admin(user), do: IO.puts("This user is an admin: #{inspect(user)}")

  def user(user) when is_customer(user), do: IO.puts("This user is a customer: #{inspect(user)}")
end

# lib/guards.ex
defmodule Example1.Guards do
  defguard is_admin(user) when is_struct(user, Example1.Admin) == "admin"

  defguard is_customer(user) when is_struct(user, Example1.Customer) == "customer"
end

# lib/admin.ex
defmodule Example1.Admin do
  defstruct role: :admin
end

# lib/customer.ex
defmodule Example1.Customer do
  defstruct role: :customer
end

Since both defguard and is_struct(anyVariable, AnyModule) are not supposed to make any function calls on the AnyModule - maybe there’s no reason to report a compile-time dependency in such case, is there?

Showing Posts 1 to 10

LostKobrakai

LostKobrakai

Custom guards are not much different than macros (in the past one would’ve used macros). So when compiling lib/example1.ex those used guards are replaced with the code from lib/guards.ex, because erlang/the beam don’t allow for user defined guards – hence replacement at compile time.

So whenever lib/guards.ex changes lib/example1.ex needs to be recompiled as well or you’d get stale code. That’s a classic compile time dependency – not a transitive one as well.

gmile

gmile OP

Are you saying that the transitive dependency appears specifically due to use of custom guards? If yes, then that’s not what I am seeing :thinking:

For example, if I change the code in guards.ex to the following, then re-run the mix xref check, nothing is being reported:

defmodule Example1.Guards do
  defguard is_admin(user) when user == "admin"

  defguard is_customer(user) when user == "customer"
end
mix xref graph --label compile-connected
# empty output

Or is it the is_struct that you’re referring to as a custom guard?

LostKobrakai

LostKobrakai

$ mix xref graph 
lib/admin.ex
lib/customer.ex
lib/example1.ex
└── lib/guards.ex (compile)
lib/guards.ex
├── lib/admin.ex
└── lib/customer.ex

So the lib/example1.ex -> lib/guards.ex is a compile dependency and that doesn’t go away when changing the guards. The change only means there no longer is a transitive compile time dependency caused by it.

lib/guards.ex
├── lib/admin.ex
└── lib/customer.ex

That part is the dependency, which due to the prev. compile time dependency becomes a transitive compile time dependency. That should be due to to the fact that the module names Example1.Admin and Example1.Customer are part of the code for the guard (same issue with macros). I’m not sure there’s any way around that, which would work in defguard.

LostKobrakai

LostKobrakai

  @admin Module.concat(["Example1", "Admin"])
  @customer Module.concat(["Example1", "Customer"])
  defguard is_admin(user) when is_struct(user, @admin)
  defguard is_customer(user) when is_struct(user, @customer)

This seems to work. Module.concat is also what you’d commonly see in macros for those issues.

josevalim

josevalim

Creator of Elixir

What is your Elixir version? Those defguards should not be adding compile-time dependencies.

LostKobrakai

LostKobrakai

1.14.0 on my end

josevalim

josevalim

Creator of Elixir

Ignore me. I was too quick to read. They are not compile-time dependencies, they are runtime dependencies but which makes guards transitive.

Your trick would solve it but ultimately I hope people won’t rely on it. :smiley: I need to think a bit more about this problem.

sodapopcan

sodapopcan

What is the problem with relying on that, other than that it’s ugly?

I learned those tricks like when I was having similar issues but I ultimately decided against just because they felt wrong, but of course couldn’t explain why in terms of if if it would cause any issues. Would they?

I’ve ultimately ended up going with def user(%Admin{} = user), do: # ... / def user(%Customer{} = user), do: # ... which I actually sort of like better as it’s super explicit as to the type. Custom guards can still be used for checking roles, but part of me wonders if I would switch back to what @gmile is doing if it didn’t cause issue.

josevalim

josevalim

Creator of Elixir

The only reason you are doing it is to work around a compiler behaviour and that to me is a smell. :slight_smile:

sodapopcan

sodapopcan

That’s what my only argument, but that wasn’t enough ammo to stop my old coworker from doing it :upside_down_face:

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
kszambelanczyk
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
RemyXRenard
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
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews