OmanF

OmanF

In the attached screenshot (taken from an exercise in “Programming Elixir”) can be shown that BugReport is defined as a struct that, itself, has as a value for one of its keys a struct: Customer.

Yet, when I tried, in iex, both the put_in function and its expanded form, when the owner key received a single string, succeeded.

My understanding of structs is that once I defined BugReport to take a Customer as the type of the key owner, it set in stone.

So, what am I misunderstanding about defining structs?

Thanks.

Showing Posts 1 to 4

peerreynders

peerreynders

defmodule BugReport do
  defstruct owner: %Customer{}, details: "", severity: 1
end

defstruct takes a keyword list. The keys describe the keys for the struct the values are the default values when the struct is created. The type of a value under a key isn’t fixed (Elixir is dynamically typed). Required keys are identified with the @enforce_keys module attribute.

You can use @type to indicate what the type should be for use with dialyzer. See this and this for the limitations of success typing.

michalmuskala

michalmuskala

Why should it fail? Elixir is a dynamically typed language - there are generally no type constraints. The values in struct declaration are about a default value when a new struct is created - they don’t mean anything more and in particular don’t affect how struct can be updated.

dimitarvp

dimitarvp

As others mentioned, Elixir is dynamically-typed.

However, you can define the structures as Ecto schemata and enforce types via the Changeset functions – even without using a database (many people wrongly assume using Ecto mandates a database). This thread has pointers on how to use Ecto without a database – maybe even better resources exist.

You can even use plain structures without Ecto wiring, as long as you pass in typing information when assigning struct fields.

Have in mind though, if you are gonna use Ecto’s Changeset, that should be the only way you manipulate the structures defined through it. No normal assigments or Access functions should be used (like put_in in your code).

IvanR

IvanR

Elixir is dynamically-typed. However, with a library like Domo, it’s possible to validate struct’s type explicitly as the following:

defmodule Customer do
  use Domo
  defstruct name: "", company: ""
  @type t :: %__MODULE__{name: String.t(), company: String.t()}
end

defmodule BugReport do
  use Domo
  defstruct owner: %Customer{}, details: "", severity: 1
  @type t :: %__MODULE__{owner: Customer.t(), details: String.t(), severity: non_neg_integer()}
end
bug_report = BugReport.new!(owner: Customer.new!(name: "OFK", company: "PragProg"), details: "It's Broke!", severity: 1)
%BugReport{
  details: "It's Broke!",
  owner: %Customer{company: "PragProg", name: "OFK"},
  severity: 1
}

bug_report.owner |> put_in("This should fail, right?") |> BugReport.ensure_type()
{:error,
 [
   owner: "Invalid value \"This should fail, right?\" for field :owner of %BugReport{}. \
Expected the value matching the %Customer{} type."
 ]}

%BugReport{bug_report | owner: "How about this?"} |> BugReport.ensure_type()
{:error,
 [
   owner: "Invalid value \"How about this?\" for field :owner of %BugReport{}. \
Expected the value matching the %Customer{} type."
 ]}

%BugReport{bug_report | details: "Updated details"} |> BugReport.ensure_type()
{:ok,
 %BugReport{
   details: "Updated details",
   owner: %Customer{company: "PragProg", name: "OFK"},
   severity: 1
 }}
— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews