Skip to content

Enforce keyword syntax for tuples of size 2 inside list#52

Open
uohzxela wants to merge 4 commits intomasterfrom
keyword-sugar-in-list
Open

Enforce keyword syntax for tuples of size 2 inside list#52
uohzxela wants to merge 4 commits intomasterfrom
keyword-sugar-in-list

Conversation

@uohzxela
Copy link
Owner

@uohzxela uohzxela commented Aug 2, 2017

This fixes issue #44. As discussed, issue #49 is a related problem but requires a different solution, so I will open a separate PR for that.

@uohzxela uohzxela requested a review from whatyouhide August 2, 2017 23:42
lib/ex_format.ex Outdated
# Tuple containers
def to_string({:{}, _, args} = ast, fun, state) do
tuple = "{" <> tuple_to_string(args, fun, state) <> "}"
inside_list? = Map.get(state, :inside_list?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect the key to be in the state so let's go with state.inside_list? instead.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually inside_list? is not always in state because I didn't add it as a default attribute when initializing the state map. That's why I have to use Map.get/2 which returns a nil if inside_list? is not found. Hence this explains why I have to do inside_list? == true, and why I didn't use the built-in syntax for map update. :P

Will add it as a default attribute to the state map during initialization if there's no objections.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add inside_list?: false when initializing the map, that's def the way to go yes.

lib/ex_format.ex Outdated
inside_list? = Map.get(state, :inside_list?)
tuple =
# Enforce keyword syntax for tuples of size 2 inside list
if inside_list? == true and length(args) == 2 do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inside_list? == true is the same as inside_list?.

lib/ex_format.ex Outdated
end

defp list_to_string(list, fun, state) do
state = Map.put(state, :inside_list?, true)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%{state | inside_list?: true}

sweet_xml: "~> 0.6",
# Statsd metrics sink client
{:statix, "~> 1.0"},
statix: "~> 1.0",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh boy this is an interesting one. We always write this as tuples for some reason :| Should we start writing them like this, which sucks because only works if you don't have options? I'm not sure how to proceed. \cc @josevalim

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, what could break if we start formatting mix.exs like this? 🤔

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anything would break, it'd just be annoying to have to rewrite everything using tuples when you wanted to add options.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, thanks @lpil. I feel that this is a minor compromise since you are getting a cleaner syntax in return. Safe to merge? @whatyouhide @josevalim

@uohzxela uohzxela force-pushed the keyword-sugar-in-list branch from a2c181b to 55b6410 Compare August 3, 2017 22:30
@whatyouhide
Copy link

whatyouhide commented Aug 4, 2017 via email

@whatyouhide
Copy link

@uohzxela so me and @josevalim were talking about this and figured out that a way to move this forward for now is to respect what the user wrote wrt tuples vs keyword syntax. I believe most of the time people write keyword syntax anyways so it shouldn't really be a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants