It is so nice to program in a sane language!

Just watched Gary Bernhardt’s “wat”. JavaScript:
image
It is so nice to work with a sane programming language
rather than this lunacy!

12 Likes

Julia isn’t exactly immune to this kind of interesting behavior. What does

function g(a)
  if a
    f() = 2
  else
    f() = 3
  end
  return f
end

do? :slight_smile: (disallow methods of local functions in different blocks · Issue #15602 · JuliaLang/julia · GitHub)

5 Likes

Yes, this one is weird. It does have a “right” way solution though:

julia> function g(a)                                           
         if a                                                  
           f = () -> 2                                         
         else                                                  
           f = () -> 3                                         
         end                                                   
         return f                                              
       end                                                     
g (generic function with 1 method)                             
                                                               
julia> g(true)()                                               
2                                                              
                                                               
julia> g(false)()                                              
3                                                              
                                                               
julia> 

The wrong way just needs to be caught by the compiler, I think.

6 Likes

By the way, if you know of some weirdness in Julia, do post it here please.

4 Likes
f = 2
f1 = 2
2f + 2f1

edit: heh, nice, discourse highlighting gives it away

14 Likes
julia> a = -1
-1

julia> 1^a === 1^(-1)
false
julia> a = 1; b = 2; a + b = 3;

julia> 1 + 4
3
julia> a = 1 + 2
           - 3
-3

julia> a
3
32 Likes

Nice! Should

julia> x + y = 2
+ (generic function with 1 method)

just be forbidden?

3 Likes
julia> a = 1; b = 2; a + b = 3                                 
ERROR: error in method definition: function Base.+ must be explicitly imported to be extended                                 
Stacktrace:                                                    
 [1] top-level scope at none:0                                 
 [2] top-level scope at REPL[10]:1    

A bit of protection…

1 Like

If you have used Base.+, in the module, yes.

3 Likes

Haha! What a fantastic thread! For readers not so familiar with Julia, most of these are mere curiosities, I’ve never had any of them creep up on me (save perhaps thinking I was adding methods to Base functions without remembering to import them first). Julia is highly consistent and sane in my experience…

4 Likes

Numbers being iterable and trailing 1 indexes can be somewhat surprising:

julia> size(1.0)
()

julia> 1.0[1, 1, 1]
1.0

julia> collect(1.0)
0-dimensional Array{Float64,0}:
1.0
9 Likes

I encountered both wats above in real-world code. The worst was when had a complicated interpolation scheme where I sampled a function f(x,y) at points 0, 1 and 2 in both directions, and wanted to compute a second derivative with (f02 - 2f01 + f00). That was a fun hour debugging why my results were wrong… (and not even completely wrong, since my function f was about 10…) In general though I agree with you, julia is pretty consistent.

8 Likes
julia> where where where where where where where
Any
26 Likes

This seems to be happening with any odd number of where greater than 1?

1 Like

It is basically

T where T where S

with the symbol where instead of T and S.

9 Likes

I always thought writing 1x and 2f instead of 1*x and 2*f was a bad idea.
As my father used to say “just because you could doesn’t mean you should”.

14 Likes

I much prefer over 2*π, which is better still than 2*pi or the ugliest =2*PI(). Also I like being able to write √(2log(2)).

But every once in a while I get carried away and try to write 2πf, which doesn’t work out so well.

4 Likes

In this case the best is to use tau :wink: .

11 Likes

How many times have I or my students been caught out by for i in n instead of for i in 1:n. I really wish the former was just made a syntax error.

9 Likes

Conversely, I often find this syntax convenient. :man_shrugging: