Unexpected behavior of #==#

Hi,
I was playing around with multi line comments (in Julia v0.5.1) , and got some interesting results.
So I tried:

julia> my#=comment=#var = 0

  • (generic function with 1 method)

And now I get:
julia> 3 * 4
0

So I just overloaded (*) for (Any,Any). That’s weird.
So, I wanted to retry in a way, that I can edit *, so:

import Base.*
my#=comment=#var = 0

This creates a new method for (*), so “hi” * false returns now 0.

When I try this in Jupyter via Juliabox (Version 0.5), I get an error, if I don’t include Base.* first, but than it also adds a new method to it.

So, does anybod know, why this happens?

Here’s my versioninfo():
Julia Version 0.5.1
Commit 6445c82* (2017-03-05 13:25 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: AMD Phenom™ II X6 1100T Processor
WORD_SIZE: 64
BLAS: libblas
LAPACK: liblapack
LIBM: libm
LLVM: libLLVM-3.9.1 (ORCJIT, amdfam10)

1 Like

It’s parsed as juxtapose, i.e. multiplication.

That seems like it is probably unexpected behavior, which probably should be changed in the parser to not count as juxtaposition.

Thank you vary much for the answer, I didn’t think of that.

I also didn’t know you can overload operators that simply.

Also a thing that surprised me, though understandable:

If I open an new julia instance and run a+b=0, it just sets this as the only method for (+). But if use (+) once before in that instance, than I can’t redefine it like that, without including Base.+, and even than it would only add a new method, not “remove” the old ones.

In my opinion, this is a flaw in Julia. It is too easy to accidentally redefine a built-in operator. Indeed, I posted an issue last year on github to gripe about my experience when I made a small typo in a function – I put an = instead of a colon somewhere, or something similar (inside an array subscript!). The consequence was that “+” was redefined for the whole function, causing the function to fail with a mysterious error on a line that was distant from the line with the typo.

I would say that redefining a built-in operator should require that the programmer write a highly obvious chunk of code, or else a syntax error should result.

On the other hand, if there’s ever an international obfuscated julia code contest, this will be a useful feature to have. :wink:

julia> import Base.-

julia> my_var = 0;

julia> my-var = my_var + 1;

julia> 4 - 2
1
2 Likes

It occurred to me as well. The problem is that this happens even without importing the function:

julia> a-b=rand(15)
- (generic function with 1 method)

julia> rand(5) .- rand(5)
5-element Array{Array{Float64,1},1}:
 [0.83553, 0.846525, 0.447319, 0.829272, 0.14231, 0.671123, 0.532965, 0.704368, 0.280074, 0.180665, 0.833187, 0.589239, 0.335998, 0.216206, 0.992392]       
 [0.269979, 0.313953, 0.313245, 0.969569, 0.772667, 0.934492, 0.93995, 0.188244, 0.101232, 0.0525931, 0.12574, 0.692653, 0.471061, 0.366899, 0.462796]      
 [0.334985, 0.12022, 0.0730848, 0.65934, 0.588324, 0.160928, 0.274765, 0.267733, 0.730062, 0.253658, 0.837493, 0.00747138, 0.983055, 0.290284, 0.27298]     
 [0.000187563, 0.605232, 0.0321455, 0.212838, 0.103809, 0.557014, 0.625323, 0.266018, 0.363476, 0.591861, 0.544453, 0.17569, 0.769616, 0.00455865, 0.395421]
 [0.781084, 0.348457, 0.523047, 0.727615, 0.375016, 0.674879, 0.328631, 0.942764, 0.827079, 0.860394, 0.845891, 0.416686, 0.125326, 0.723758, 0.603509]

I expected to get a 5-element array of scalars, not an array of arrays, it took me a while to understand what was going on.

Actually, I can swear that when I defined the variable I didn’t even get in the REPL the message about (re-)definition of the method, but I couldn’t reproduce the issue afterwards (I should have taken a screenshot :frowning: )

Ref Infix operator definition syntax needs documentation · Issue #15483 · JuliaLang/julia · GitHub, https://github.com/JuliaLang/julia/issues/16383

1 Like

This one’s also interesting: Let’s say I want t to be true if a<=b and b==3, but I make a typo.

julia> versioninfo()
Julia Version 0.6.0-pre.alpha.263
Commit 55f44179b9 (2017-03-27 00:01 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i5-4690K CPU @ 3.50GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

julia> t = (a <= b = 3)
3

julia> 1<=2

signal (11): Segmentation fault: 11
while loading no file, in expression starting on line 0
unknown function (ip: 0x31b7f2e83)
Allocations: 2846087 (Pool: 2844545; Big: 1542); GC: 3
Segmentation fault: 11

That shouldn’t crash and looks like a more fundamental problem with assigning function definitions. Reported as https://github.com/JuliaLang/julia/issues/21172.