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
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:
6
Also Liked
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.
4
crabonature
def takesStruct(%StructTest{} = struct) do, means:
- this function will be invoked only when you pass to it one argument which is
%StructTest{}struct, otherwise it will not be invoked - when you invoke it with proper struct, this struct will be bound to
structvariable, which you can use later in the function body.
4
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.
3
Popular in Questions
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
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
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Other popular topics
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
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
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
I would like to know what is the best IDE for elixir development?
New
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
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New







