mmport80

mmport80

Type Checking Structs

Structs’ success typing is the same as common garden maps to Dialyzer, right?

What are the preferred ways to enforce a particular struct?

Marked As Solved

michalmuskala

michalmuskala

Erlang 19 introduces more advanced support from dialyzer for maps -
checking specific keys and general key types. Support for those new
features is in Elixir 1.3 as well. Here’s the PR that introduced them:
https://github.com/elixir-lang/elixir/pull/4662

Also Liked

easco

easco

In Elixir you can pattern match on the struct’s type so for example:

defmodule StructTest do
    defstruct name: "Foo!"
end

defmodule TestStruct do
    def takesStruct(struct = %StructTest{}) do
        IO.puts(inspect struct)
    end
end

The takesStruct function will only match for instances of the %StructTest{} structure.

crabonature

crabonature

def takesStruct(%StructTest{} = struct) do, means:

  1. this function will be invoked only when you pass to it one argument which is %StructTest{} struct, otherwise it will not be invoked
  2. when you invoke it with proper struct, this struct will be bound to struct variable, which you can use later in the function body.
Qqwy

Qqwy

TypeCheck Core Team

You can see more information at the Typespecs page in the Documentation.
To match a Struct inside a typespec, use the normal %StructName{} syntax.

EDIT: The documentation moved to a new location.

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

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
New

We're in Beta

About us Mission Statement