Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuse maps and tuples for printing #14079

Merged
merged 1 commit into from
Dec 18, 2024
Merged

Conversation

gldubc
Copy link
Member

@gldubc gldubc commented Dec 17, 2024

For printing types (tuples and maps), we do a general pass that fuses elements that differ by one element or less.

Example:
{:ok, integer()} or {:ok, float()} is shown as {:ok, float() or integer()}
%{..., a: float()} or %{..., a: integer()}is shown as %{..., a: float() or integer()}

and

:error or
  ({%Decimal{coef: :inf, exp: integer(), sign: integer()}, binary()} or
    {%Decimal{coef: :NaN, exp: integer(), sign: integer()}, binary()} or
    {%Decimal{coef: integer(), exp: integer(), sign: integer()}, term()} or
    {%Decimal{coef: :inf, exp: integer(), sign: integer()} or
        %Decimal{coef: :NaN, exp: integer(), sign: integer()} or
        %Decimal{coef: integer(), exp: integer(), sign: integer()}, term()})

is shown as

:error or
 ({%Decimal{coef: integer() or (:NaN or :inf), exp: integer(), sign: integer()}, term()} or
    {%Decimal{coef: :NaN or :inf, exp: integer(), sign: integer()}, binary()})
)

)
|> dynamic()
|> to_quoted_string() ==
"dynamic(\n :error or\n ({%Decimal{coef: integer() or (:NaN or :inf), exp: integer(), sign: integer()}, term()} or\n {%Decimal{coef: :NaN or :inf, exp: integer(), sign: integer()}, binary()})\n)"
Copy link
Member

@ericmj ericmj Dec 17, 2024

Choose a reason for hiding this comment

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

Can we remove the parenthesis in integer() or (:NaN or :inf) by building the or AST with the operator's natural associativity?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not very easily, here the parenthesis appears because the AST for singleton atoms is built separately (before) the AST of all types is built

unions -> unions |> Enum.sort() |> Enum.reduce(&{:or, [], [&2, &1]})

Those won't appear when printing for instance three singletons :NaN or :inf or :foo

Copy link
Member

Choose a reason for hiding this comment

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

@gldubc we can probably do it by swapping the order we build the union. I will take a look at if before or after I merge. :)

@josevalim josevalim merged commit bcc36c5 into elixir-lang:main Dec 18, 2024
7 of 9 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

josevalim pushed a commit that referenced this pull request Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants