Today I Learned

Revealing struct information

In Elixir when we are printing struct we can hide struct’s details implementing Inspect protocol. For example the DateTime struct is doing that so when we are inspecting it it shows us pretty version instead of the detailed one.

iex(1)> now = DateTime.utc_now()
~U[2020-10-06 10:59:58.388743Z]
iex(2)> IO.inspect(now)
~U[2020-10-06 10:59:58.388743Z]

If we want to reveal all the information that the struct contains we can use structs: false option.

iex(5)> IO.inspect(now, structs: false)
  __struct__: DateTime,
  calendar: Calendar.ISO,
  day: 6,
  hour: 10,
  microsecond: {388743, 6},
  minute: 59,
  month: 10,
  second: 58,
  std_offset: 0,
  time_zone: "Etc/UTC",
  utc_offset: 0,
  year: 2020,
  zone_abbr: "UTC"