JuliaLang/julia

the .. operator

Closed this issue · 9 comments

is the behavior of the .. variable intentional?

I found this because I wanted to see whether I could propose that x..y(...) could be syntax for y(x, args...) or possibly instead syntax that x..y == Intrinsics.getfield(x,y) and x.y == Base.getdot(x,y)

julia> x..y = y:-1:x
# methods for generic function ..
..(x,y) at none:1

julia> 1..7
7:-1:1

julia> .... = 1
1

julia> ....
1

julia> x....y = x+y
ERROR: syntax: malformed function argument (call * .... y)

julia> x.....y = x+y
ERROR: syntax: malformed function argument (. .... 'y)

julia> x......y = x+y
ERROR: syntax: malformed function argument (call * x ....)

julia> x.......y = x+y
ERROR: syntax: extra token after end of expression

I don't know, what should it do?

Syntax error? .. and .... aren't exactly in the common list of valid variable names, although they seem great for obfuscating code contests

Or reserve them for something useful, such as my other two proposals (making x..y(args...) OO syntax for y(x, args...), or making x..y == Intrinsics.getfield(x,y) and x.y == Base.getdot(x,y) which is overloadable and defaults to Intrinsics.getfield)

The contest we should have is how many colons and dots you can have in one julia expression.

Since .. is a sideways colon, it ought to be the second most popular symbol.

Also .... is not that strange a variable name. In lisp, the empty string can be a variable name :)

Lisp is so cool

I added a syntax error for 4 or more dots.
Although everybody wants a.b syntax for various things, I think a..b is actually worthless. Might as well have it available as an operator in case some use arises, but I don't think anybody would be happy with x..f(y) for method calls.

How about using a..b as a shortcut to a[:b]? I have the feeling the dictionary-like types with symbols as keys are being used a lot (I'm thinking in DataFrames and PyCall). This is not as powerful as an overloadable dot operator, but maybe it is much simpler/faster to implement...

Clever idea, but overloading the dot operator is going to happen, so it becomes a bit of a waste of syntax.