Elixir: Formatter do not add parenthesis for the typespec

Created on 27 Feb 2018  路  4Comments  路  Source: elixir-lang/elixir

Environment

  • Elixir & Erlang versions (elixir --version): Elixir 1.7.0-dev (77c9d48) (compiled with OTP 20)
  • Operating system: MacOS

Current behavior

I ran mix format I am expecting this line https://github.com/straw-hat-team/straw_hat_mailer/blob/224410cf37ee685237bc058c4822361e6b705f4d/lib/straw_hat_mailer/schema/template.ex#L76 do not change

Expected behavior

I am expecting the typespec to be added the parenthesis as Elixir does with the primitive types (so far).

So it would change to

@spec changeset(t(), partial_attrs()) :: Ecto.Changeset.t()

Most helpful comment

Parens can't be added there because as it's written now, that t could be a reference to either a function _or_ a variable in that scope. Adding parens could break if it was supposed to be a reference to a variable.

If something is a function call outside of the current scope, you can be sure that it's not a reference to a variable, so you can safely add the parens.

All 4 comments

Elixir formatter cannot change the AST/semantics of the code, otherwise we risk breaking the code. For this reason, we only add parens to remote calls, we cannot add to variables.

I know from context you know it is a spec but the formatter does not have such context.

@josevalim if that is true why does the formatter add the parenthesis when I remove it from Ecto.Changeset.t what does we only add parens to remote calls means? For my personal knowledge, if you dont mind to answer.

With Ecto.Changeset.t we know it has to be a function call so we can add the parenthesis. For t it can be either a function call or a variable so we could break code if parenthesis are added.

Parens can't be added there because as it's written now, that t could be a reference to either a function _or_ a variable in that scope. Adding parens could break if it was supposed to be a reference to a variable.

If something is a function call outside of the current scope, you can be sure that it's not a reference to a variable, so you can safely add the parens.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cmeiklejohn picture cmeiklejohn  路  3Comments

GianFF picture GianFF  路  3Comments

ckampfe picture ckampfe  路  3Comments

josevalim picture josevalim  路  3Comments

chulkilee picture chulkilee  路  3Comments