kip
TLDR;
What should I emit for inspect/2 output of library structs? Executable code, string representation or default (outputs the struct fields)?
Summary
In April, I changed the output of inspect/2 for Cldr.LanguageTag.t to be executable code rather than the default structural output. I did this to align with what I understood is the emerging pattern for Elixir structs but not everyone agrees.
I decided to review the inspect/2 output for all the Elixir core structs (summary below)
It appears, as I had understood, that most structs emit executable code these days. Using a sigil if one is part of kernel and a function call if not.
The ones that do not emit executable code appear to do so because there is no mapping from the struct to code - perhaps because the struct holds some state like File.Stream. And it makes sense for a PID to be represented a a string because the struct is opaque. I think URI might be an outlier - it could be URI.parse!(uri_string) I think, without losing any information.
I didn’t seen anything in the Elixir Antipatterns guide.
What is core teams guidance and what are community expectations for inspect/2 output for a struct?
Output executable code
- Date
- Time
- DateTime
- Date.Range
- NaiveDateTime
- Regex
- MapSet
- Range
- Exceptions (emit their name only, which I count as code)
Output structure
- URI
- File.Stream
- Version
Output string representation
- PID
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 14 Posts
Marcus
I expect executable code as a result of
inspect. In my projects, I also change it to this effect.If I want to see the structure I use:
LostKobrakai
To me “exectuable code” inspection format is a (better) alternative to the previous convention of having
#Struct.Name<…>format. But I don’t expect it in places where no custom inspect implementation is needed and/or makes sense, so I don’t consider that new pattern a general one to be applied everywhere.josevalim
The standard library is not a great example because Date, Time, Regex, Range, etc, all have built-in sigils and operators, imported by default, which are compact and justify using the same notation for printing them. If we remove those from the equation, we end-up with:
MapSet.newGiven providing built-in sigils/operators is not practical outside of stdlib, here is the main chart:
#PID<...>)acalejos
@josevalim This is a very useful breakdown and I’m glad the original question was asked.
Would this make sense in the documentation somewhere? Maybe under Library Guidelines?
josevalim
Perhaps we could document it under Inspect itself?
acalejos
I think that’d be appropriate
kip
José, thank you, I think these are a good addition to the
inspect/2docs.I’m probably overthinking it again but I’m not able to reconcile the “If yes (expose the struct without leaking implementation details), then output the struct” with the Date/Time/DateTime structs.
Those don’t appear to include implementation details and yet they output executable code - albeit in sigil form. (I agree that sigil form is only really appropriate for stdlib since the those sigils are included by default).
tfwright
This seems to be what makes them exceptions, but also aren’t structs just as executable as sigils? I’m a bit confused by that category. I can definitely see the advantage of emitting executable code over a string representation of the code, which is why I generally prefer the actual struct vs a “pseudo code” string representation, but I don’t see the downsides to emitting structs when they are public. Even if they leak implementation details I think I would prefer that as long as there is an additional convention around signalling that.
On the other hand it’s not clear to me why MapSet returns what it does so it’s likely I’m missing something.
kip
Thats definitely part of my consideration here. There is an argument (going on in my head) about whether
Cldr.LanguageTag.tshould be opaque. In which case the inspect output would not be the structure.When the struct is really large then, at least in iex, the output isn’t always clarifying. At least thats been part of my thinking.
LostKobrakai
MapSet is an opaque type. It means the internal representation might change at any time – even has in the past, so not just a theoretical thing. Therefore it cannot show you the internals, because then you start depending on it (like pasting from one elixir version to the next might break), which is something to be avoided.
Previously it did inspect as
#MapSet<[…]>, which made the mapset a comment in elixir syntax, meaning you couldn’t paste the inspected value into a shell or file and have elixir turn it back into an actual MapSet.To help with that downside many of such structs were switched to an “executable code” representation, which is still valid elixir code, which happens to evaluate to the inspected value without needing to know the internals. That’s
MapSet.new([…]).Where it gets more into the space of “tradeoffs” is when it’s not clearly a opaque type, like with
Decimal. It is a public and documented struct, but who want so see%Decimal{exp: -4, sign: 1, coef: 152345}vs.Decimal.new("15.2345")in the common case – for the uncommon case there’s alwaysinspect(…, structs: false).Decimalapi for any manipulation on a decimal.From the issue @kip linked it seems those bullet points might not apply to how one would interact with a language tag in cldr.