Is there an elixir function that prints out the type of a value and display its fields?

Is there an elixir function to tells the type of a value and display its fields(if it has any)?

For instance I am leaning Plug and want to know what type conn is and display its fields.

One more thing does Elixir have structs or objects as they are understood in languages like C or Pascal?

1 Like

inspect(blahSomething) returns a string representation of whatever blahSomething is.

IO.inspect(blahSomething) prints the string representation of whatever blahSomething is to the standard output.

All of this is documented in the manual too. :slight_smile:

Elixir’s structs are like structs in pascal or c yes, underneath they are just statically keyed maps, they are not packed in memory as in pascal or c though, especially since memory is not really a concept on the EVM, it manages it for you.

There is no static typing, however the dialyzer tool (also detailed in the manual) is an awesome tool that runs over your code and checks the success typing of your program, Dialyxer is a dead-simple Elixir interface for using it and I highly recommend it. :slight_smile:

2 Likes

See the the i/1 helper docs in IEx:

Prints information about the data type of any given term.

## Examples

    iex> i(1..5)

Will print:

    Term
      1..5
    Data type
      Range
    Description
      This is a struct. Structs are maps with a __struct__ key.
    Reference modules
      Range, Map

Example:

iex(19)> i File.stat!("README.md")
Term
  %File.Stat{access: :read_write, atime: {{2017, 3, 31}, {21, 24, 2}}, ctime: {{2017, 3, 31}, {18, 56, 52}}, gid: 20, inode: 10353339, links: 1, major_device: 16777220, minor_device: 0, mode: 33188, mtime: {{2017, 3, 31}, {18, 56, 52}}, size: 2879, type: :regular, uid: 501}
Data type
  File.Stat
Description
  This is a struct. Structs are maps with a __struct__ key.
Reference modules
  File.Stat, Map
Implemented protocols
  IEx.Info, Inspect
11 Likes