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

Broadcasting \sqrt (√) (and similar infix ops?) #23814

Open
tkoolen opened this issue Sep 22, 2017 · 11 comments
Open

Broadcasting \sqrt (√) (and similar infix ops?) #23814

tkoolen opened this issue Sep 22, 2017 · 11 comments
Labels
broadcast Applying a function over a collection parser Language parsing and surface syntax

Comments

@tkoolen
Copy link
Contributor

tkoolen commented Sep 22, 2017

From https://discourse.julialang.org/t/using-with-a-vector/6025.

x = rand(3)
(x)

results in

WARNING: sqrt{T <: Number}(x::AbstractArray{T}) is deprecated, use sqrt.(x) instead.

sqrt.(x) works fine, but √.(x) results in

ERROR: syntax: invalid identifier name "."

since √ is an infix op (so .√(x) should be used, or (√).(x)).

Could √.(x) be made to work?

@stevengj
Copy link
Member

See #20249. The following works:

julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> .√a
3-element Array{Float64,1}:
 1.0    
 1.41421
 1.73205

(Since it is an operator, you add the dot before the operator, just like .+.)

@tkoolen
Copy link
Contributor Author

tkoolen commented Sep 22, 2017

I already mentioned that in the issue description, but wouldn't it make sense to also support √.(x)?

@mschauer
Copy link
Contributor

mschauer commented Sep 22, 2017

As binary operators o take their arguments at both sides, x .o y is not really different from y o. x. For unary operators o. x is less surprising than .o x and could be allowed as well, especially when it is not clear if a symbol is an operator or just an exotic character.

@fredrikekre
Copy link
Member

#20540

@mschauer
Copy link
Contributor

Ah, #19089 landed after #20540 went dormant.

@Keno
Copy link
Member

Keno commented Sep 22, 2017

I think √.(x) should parse and work regardless of the fact that it's a unary operator. That's just the standard call syntax. E.g. even though it's an operator you can do

julia> √(a,b) = 1
√ (generic function with 1 method)

julia> √(1,2)
1

which you can't express in operator syntax, but can express in call syntax.
Whether to make the do work on the other side is a separate question.

@kshyatt kshyatt added the broadcast Applying a function over a collection label Sep 29, 2017
@Sacha0
Copy link
Member

Sacha0 commented Nov 18, 2017

Making √.(x) work requires a breaking change IIRC, so labeling triage. Best!

@Sacha0 Sacha0 added the triage This should be discussed on a triage call label Nov 18, 2017
@JeffBezanson JeffBezanson added the parser Language parsing and surface syntax label Nov 19, 2017
@StefanKarpinski
Copy link
Member

Why? √.(x) is a syntax error currently.

@Sacha0
Copy link
Member

Sacha0 commented Nov 20, 2017

Why? √.(x) is a syntax error currently.

A vague memory from #20540. But having briefly reviewed #20540 I believe you are correct, i.e. making op.(x) parse for unary op shouldn't involve breakage. (The breakage in #20540 seems to follow from making op. parse for binary op, as e.g. 1+.2 becomes ambiguous as 1.+2 is now.) Best!

@Sacha0
Copy link
Member

Sacha0 commented Nov 20, 2017

Hm, operators that are simultaneously unary and binary might add a twist though...

@stevengj
Copy link
Member

stevengj commented Nov 20, 2017

No, I don't think simultaneous/unary binary ops should matter. If you make op.(x...) parse as (op).(x...) then it will work in both cases.

+.(3) and 4+.(3) are both currently syntax errors too, so there is no breakage here.

@StefanKarpinski StefanKarpinski removed the triage This should be discussed on a triage call label Nov 20, 2017
@StefanKarpinski StefanKarpinski added this to the 1.x milestone Nov 20, 2017
@DilumAluthge DilumAluthge removed this from the 1.x milestone Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcast Applying a function over a collection parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

10 participants