My wishlist for the next version of Julia

I just tested sort(A, dims=1). All rows are mismatched now. Be very careful about using it.

A =
8 8 6
4 8 7
3 7 3
10 10 1
9 5 5

sort(A, dims=1) gives me the below:
3 5 1
4 7 3
8 8 5
9 8 6
10 10 7

@GunnarFarneback I’m curious, this is proper syntax in Matlab? What does it mean?

1 Like

From matlab’s webpage:

function [y1,...,yN] = myfun(x1,...,xM) declares a function named myfun that accepts inputs x1,...,xM and returns outputs y1,...,yN . This declaration statement must be the first executable line of the function. Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscores.

So in their example:

function [m,s] = stat(x)
    n = length(x);
    m = sum(x)/n;
    s = sqrt(sum((x-m).^2/n));

[m,s] is a way of indicating which of variables in the body of the function should be returned to the caller.
I always found this Matlab syntax ugly.


It’s used when defining functions to specify what variables are returned.


function [A, B] = f1(x, y, z)

A = % something
B = % something else

end % no return statement, A and B are returned

In Julia:

function f1(x, y, z)

    A = # something
    B = # something else
    return (A, B)


I think it’s appealing to see both the function signature and what it returns in one line (particularly when more descriptive names are used than just A and B). Many people do this in docstrings in Julia, e.g., f1(x, y, z) -> A, B, or something similar.


sort(A, dims=1) sorts each column separately; it is not equivalent to Matlab’s sortrows. The closest analogue of sortrows(A) from Matlab is sortslices(A, dims=1) in Julia.


Aside from the wonderful suggestions of the community, I’ve moved the thread out out “Internals & Design” to “General Usage”, since most of the questions in your list are already quite possible, except that they use different syntax than Matlab. To that regard, please note that the general feel of julia is done and probably won’t change much in the feature (though genuinely new feature requests are of course still welcome!) :slight_smile:

Also, see this thread for a bit more info about this:


What exactly is the difference between


if any?

1 Like

The precedence of the operators, importantly, in this case:

julia> 2 > 1 && 4 > 3

julia> 2 > 1 & 4 > 3

julia> (2 > 1) & (4 > 3)

(& and && are not the same thing)


The one in the middle is so confusing. How come the result is false?

It checks 1 & 4 first, and it is a Bitwise operation - Wikipedia, which returns 0 in this comparison. You get the expected and and or behavior only if comparing to booleans (which is what the parenthesis guarantee there).


I should point out that you can write return A,B or just

A, B

if the OP dislikes return keyword so much.

1 Like

The syntax closest to Matlab’s [a,b] = f(x,y) is probably

julia> f(x,y) = (2x+y, 4y^x)
f (generic function with 1 method)

julia> (a,b) = f(2,3)
(7, 36)
1 Like

Well, yes, but this wasn’t about calling a function but defining it.

I see. OP wants

function [a,b] = f(x,y)
    a = 2x+y
    b = 4y^x

Not an improvement, IMHO.


In Matlab, an array of objects is returned: hence [a, b] = .... In Julia, a tuple is returned.
So it would read (a, b) <- f(x) = (x, 2*x), perhaps?

I do appreciate the information available from the listing of the arguments in the place where the function begins to be defined. However, in Matlab this is then spoiled immediately since the named outputs can be assigned value anywhere inside the function! Not easy to read.


There is potentially a big difference between Matlab’s [a,b] = fm(...) and Julia’s a,b = fj(...).

In Matlab it’s possible to have internal conditional code to not compute b if the function is called as a = fm(...). This is not possible in Julia.


Note that & is a bitwise and operator. What is evaluated in 2 > 1 & 4 > 3 first is 1 & 4:

julia> 1 & 4

But note the type of the result

julia> typeof(1 & 4)

What is evaluated subsequently is

julia> 2 > 0 > 3

For those who wonder how this works, you have a magical variable nargout in your function, which you can check to see how many outputs the caller will receive, like this:

Similarly you have a nargin variable available for the input arguments, which can be used to assign default values to left out arguments, or other transformations of the arguments, e.g.

I’m particularly happy about having left that language feature behind me.


Oh no, let’s not go there!


my limited explore of MATLAB has told me that every function has 100 lines preamble to check nargin and nargout which is just… why :frowning: