tadiou

tadiou

I was just going through some stuff with some jr. devs about how structs work, and we were fooling around with what can be done with them as an exercise, and then… well. We struct (lol) something I wasn’t able to explain away. Assume:

defmodule Cat do
  defstruct type: nil, weight: nil
end 

Obviously, %Cat{variance: "tabby"} fails with keyerror, because variance doesn’t exist on the struct, and if you did go %Cat{}, you’d receive %Cat{type: nil, weight: nil}… And further if you try to go with

%{%Cat{} | variance: "tabby"}

again, it checks that you can’t add something that was defined. You get some semblence of safety when it comes to the values of what you can expect on a struct. On defined Cat struct, it’d contain the values of type and `weight.

But… Map.delete(%Cat{type: "Maine Coon"}, :weight) gives you the appearance of a valid(?) struct still.

%{__struct__: Cat, type: "Maine Coon"}

At no point does it give the indication that it’s out of step with the struct and I haven’t been able to find out how to validate that. I’d expect on create time we’d handle the checks, but when modifying post-create, there’s no integrity checks here. I thought that maybe Kernel.struct/2 might be a use to validate here, but it comes out clean.

Obviously this is a much smaller issue when you’re using Ecto and changesets, but it was something that really quirked me a bit! TIA for the explaination.

Showing Posts 1 to 10

Nicd

Nicd

Structs are only maps underneath, with some compiler guarantees. In this case, the compiler cannot infer that the deletion should not happen, so it cannot warn about it. In theory the Map.delete function could check if the given map has a :__struct__ key at runtime, but currently it doesn’t do that (and I don’t think it will be changed).

axelson

axelson

Scenic Core Team

The fact that you can see the :__struct__ key in the inspect output is meant to warn you that you no longer have a valid %Cat{} struct.

You get a similar error if you add a non-existant key to a struct directly with Map.put:

iex> Map.put(cat, :some_bad_key, "val")
%{__struct__: Cat, some_bad_key: "val"}
tadiou

tadiou OP

Okay this makes a bit more sense here! But then…

iex> %Cat{type: "Maine Coon"}.__struct__                           
Cat
iex> %Cat{type: "Maine Coon"}                                      
%Cat{type: "Maine Coon", weight: nil}

It’s not present in the output, and obviously that’s the namespacing, but if you tried to pattern match on it, you’d ultimately find that __struct__ matched correctly right? Like

iex> %{__struct__: Cat} = %Cat{type: "Maine Coon"}
%Cat{type: "Maine Coon", weight: nil}

And just for thoroughness, we’d expect that

iex> %{__struct__: Cat, variance: "Tabby"} = %Cat{type: "Maine Coon"}
** (MatchError) no match of right hand side value: %Cat{type: "Maine Coon", weight: nil}

Which you’d expect because you’re ultimately trying to initialize a __struct__: Cat.

So, we can validate visually by the output, but how would you do that programatticaly in this case?

Sebb

Sebb

Just don’t break the structs, Elixir is a dynamic language and requires some countenance.

al2o3cr

al2o3cr

It’s present, that’s how inspect knows to format it as %Cat{...} instead of %{...}.

You can even write code that matches on __struct__:

def thing_that_captures_struct(%struct_var{} = input) do
  # if you pass in a %Cat{}, "input" will be bound to the atom Cat
end

Nitpick: since this has a complex expression on the left-hand side, “initialize” is not quite the right verb - as the error message says, this is a failure to match. This would be fine:

%{__struct__: Cat} = %Cat{type: "Maine Coon"}

since the match requires all the keys in the map on the left to be present in the map on the right.

tadiou

tadiou OP

Sorry if I wasn’t being clear (it seems like it’s been a thing yesterday),

It’s present

You’re right, the __struct__ exists in both places.

iex> %Cat{} = Map.delete(%Cat{}, :type) 
%{__struct__: Cat, weight: nil}
iex> %Cat{} = %Cat{}
%Cat{type: nil, weight: nil}

But that’s again, kinda getting at the base of my problem. Given that both have __struct__ as part of what it is, and the fact that you can only see it in the output that it’s incorrect.

The struct, obviously, is syntactical sugar over map, but, is there just no comparison/matching that exists that would tell you that the struct that’s been altered by deleting a key does not actually match the same struct?

al2o3cr

al2o3cr

You could do the same thing the Inspect protocol does: compare the keys in the input with the keys from the module’s __struct__() function (which generates the same thing as writing a struct literal with no keys):

https://github.com/elixir-lang/elixir/blob/898d80e46c78b006cc1b951208e53fe7c67c02a1/lib/elixir/lib/inspect.ex#L440-L454

IMO this is a “why is it so hard to put my finger in the live electrical outlet” problem - it’s hard to deal with broken structs because you really shouldn’t be making them…

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

And in my experience they’re pretty hard to make in real world code by accident. I’ve been coding Elixir full time since structs were added to the language and I can’t think of any time this has been an issue for me.

tadiou

tadiou OP

I mean, yeah, obviously, don’t break structs, but are there any facilities for actually determining that a struct is broken once it is?

paulanthonywilson

paulanthonywilson

Just for fun you could write a function based on the Inspect.Protocol referenced by @al2o3cr

  def struct_or_bluff(%{__struct__: module} = accused) do
    if Map.keys(module.__struct__()) == Map.keys(accused) do
      :struct
    else
      :bluff
    end
  end

  def struct_or_bluff(_), do: :not_even_pretending

(but don’t)

Incidentally

%{%Cat{} | variance: "tabby"}

doesn’t fail because of much to do with it being a Struct or not. It fails because the key doesn’t exist.

%{%{} | variance: "tabby"}

also fails

Map.put(%Cat{}, :variance, "tabby")

succeeds and also produces one of those “invalid structs” (that you will never really have to worry about as @benwilson512 mentioned).

— All posts loaded —

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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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

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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews