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

Automatic broadcasting syntax f.(A) is inconsistent #22501

Closed
ZacCranko opened this issue Jun 24, 2017 · 3 comments
Closed

Automatic broadcasting syntax f.(A) is inconsistent #22501

ZacCranko opened this issue Jun 24, 2017 · 3 comments
Labels
won't change Indicates that work won't continue on an issue or pull request

Comments

@ZacCranko
Copy link

ZacCranko commented Jun 24, 2017

I have been out of the loop for a little bit and so I realise I am a little bit late to the discussion #15032, but having seen the f.(A) syntax at JuliaCon it strikes me as inconcistent. I'll try to explain why.

The infix notation A .+ B has the . prefixing the function symbol +, so maybe you are saying to yourself "this is not a problem, infix notation is just somehow special" and lines of code such as

 A .+ B .* sin.(C)

are really just fine and all is well in the universe... until you realise that these infix functions can also be written as prefixes, e.g.:

(+)(x::T, y::T) where {T<:BitInteger} = add_int(x, y)

So now what should be the correct way to broadcast +? Is it (+).(x, y) = broadcast(+, x, y),
or is it (.+)(x, y) = broadcast(+, x, y)? Currently both of these work (+.(x, y) does not, cf. #22498).

Hopefully by now I have convinced you of the problem. Next is to convince you that the solution to this problem is to switch to .f(A) and not to change the broadcast infix operators to +..

Dot postfix on operators: A +. B

Pros:

  • Consistent with function broadcasting f.(A)
  • Operations like x*.abs.(y) are easier to read (sorta?)
  • Inconsistent with many other popular programming languages whose names I don't need to mention here (Julian recalcitrance)

Cons:

  • Undefined (unattractive?) for the in-place operations: Should it be i +.= 1 or i +=. 1? Maybe both are undesirable.
  • .(A) is kind of meaningless as an object on its own, cf. .f with respect to .+
  • A *. 2 will be confused with A * 0.2. My reply: "The same kind of parsing issues exist as with 2. * A, either way you have to declare some things illegal"

Dot prefix everywhere: .f(A)

Pros:

  • Consistency with infix operators
  • Reads nicely for the the in-place operations i .+= 1.
  • As pointed out by @JeffBezanson, . has the nice interpretation as a function .(f) = (args...) -> broadcast(f, args...)
  • Consistent with many other popular programming languages

Cons:

  • Confusing to parse visually in expressions like x.*.abs(y). My reply: "vectorised expressions are always going to be ugly and hard to read, wachagonnado".

Personally I prefer the second solution, but I think either would be superior to current situation.

@yuyichao
Copy link
Contributor

yuyichao commented Jun 24, 2017

What's the problem? .+ isn't an operator anymore and we will make using it as operator no longer work if we haven't already. There's no inconsistent between operator and function either since the dot is on the call and not the function.

@yuyichao yuyichao added the won't change Indicates that work won't continue on an issue or pull request label Jun 24, 2017
@nalimilan
Copy link
Member

As you say, it's really late for this kind of discussion. If that can help you feel better about it, we often read f.(x) as being a function call using the .( operator, which has a similar form to .+. Seen that way, it's very consistent.

@Sacha0
Copy link
Member

Sacha0 commented Jun 24, 2017

Ref. #20249 and #8450. Best!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
won't change Indicates that work won't continue on an issue or pull request
Projects
None yet
Development

No branches or pull requests

4 participants