Why does Julia have multiple ways to define functions?

there are about 20 ways Meet some mind-blowing ways to define functions in Julia: a tour of the many ways to create functions in Julia | by ZJ Dai | Medium

lol

5 Likes

It’s cool that the language gives you flexibility to do all these things (like the method of overloading) but this is in ridiculous territory!

2 Likes

actually i feel uneasy about this. I would prefer fewer ways to define functions

I honestly still find this confusing. What is happening to for that to result be 2?

julia> ([1,2]')[1,2]
2

julia> ([1,2]') * [1,2]
5

Oh, I got it. Yeah, that’s quite confusing.

4 Likes

It is an indexing operation actually :smiley: It just happens to work out with the specific numbers in this example.
So ([1,2]') just gives the “row vector” [1;;2] and the following [1,2] indexes it at position 1,2 so first row, second column which is… 2 :tada:

Consider as a sanity check:

julia> ([1,2]')[1,3]
ERROR: BoundsError: attempt to access 1×2 adjoint(::Vector{Int64}) with eltype Int64 at index [1, 3]
Stacktrace:
...

So in the end, this confusion is due to a clash of syntax between indexing and array literals that just happens to work out accidentally with the numbers in this example.

6 Likes