mynameisdaniil
mynameisdaniil

Reputation: 1178

Elixir's formatter trying to remove structs. What is going on?

I've recently been shocked by formatter's suggestion to remove structs from some places, like:

{:keep_state, touch(%Data{data | booking: booking})}

formatter suggests following replacement:

{:keep_state, touch(%{data | booking: booking})}

which is clearly incorrect. Struct here keeps us from writing something like:

{:keep_state, touch(%Data{data | booking: booking, nonexistent_field: "whatever value"})}

It will not compile, and it is good; that's why we have structs (and records) and not just maps.

Can someone explain to me what is going on, what have I missed in the world of Elixir? And how do I make formatter to stop this behaviour?

Upvotes: 2

Views: 43

Answers (1)

sabiwara
sabiwara

Reputation: 3204

The Elixir formatter itself doesn't do such a thing, but Styler (a popular formatter plugin) recently released this rewrite.

This comes in anticipation of an upcoming deprecation of the struct update syntax in Elixir 1.19, in favor of pattern-matching where the variable is defined. As explained in the PR, this reduces the syntax surface of the language and will help with type inference.

Given the context, you might want to avoid using the syntax, but Styler doesn't seem to be able to patch variable definitions, so you will have to do it yourself.

Upvotes: 3

Related Questions