Ookma-Kyi

Ookma-Kyi

Using a struct within a struct produces errors

I have the following code:

defmodule Servy.Conv do
  defstruct method: "", path: "", resp_body: "", status: nil

  def full_status(conv) do
    "#{conv.status} #{status_reason(conv.status)}"
  end

  defp status_reason(code) do
    %{
      200 => "OK",
      201 => "Created",
      401 => "Unauthorized",
      403 => "Forbidden",
      404 => "Not Found",
      500 => "Internal Server Error"
    }[code]
  end
end

I get a “could not find struct” error when I update the full_status function to def full_status(%Conv = conv) do, even though the struct is defined above. Additionally, Visual Studio Code mentions

Cyclic module usage

When I try to do this. Any ideas?

Most Liked

garrison

garrison

I agree this behavior is confusing; I also think it would be more intuitive to alias the current module into itself. I can’t explain exactly why, it’s just less surprising for some reason.

With that said, obviously there is no changing this now, so it is what it is.

Structs are just maps with an extra __struct__ key set to the module name. Functions are imported into the scope, but structs are not. You can only alias the module name.

Note however that defining a nested module aliases that module within the parent, so you can do this:

defmodule A do
  defmodule B do
    defstruct [:foo, :bar]
  end
  def foo(%B{}), do: true
end

Even though the actual name of B is A.B. Which only makes it more confusing tbh.

gregvaughn

gregvaughn

Yes, but it’s even more fundamental than that. The struct “name” is a module name. Even if you were not creating a struct, but in any module you cannot refer to the current module by the leaf part of its name. For example, if you were writing any function within module Foo.Bar you cannot use Bar by itself. You’d have the same 3 options I mentioned.

sodapopcan

sodapopcan

Just to put what @gregvaughn another way (which is what helped me) is that Elixir doesn’t technically have submodules. The . is a bit of an illusion and is only meaningful for constructs like alias, otherwise it’s just like any other character in a module name. Foo and Foo.Bar have absolutely no relation as far as the VM is concerned.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
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 43806 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement