# My wishlist for the next version of Julia

**URL:** https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987
**Category:** General Usage
**Tags:** question, matlab
**Created:** [October 18, 2021, 4:28pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987 "2021-10-18T16:28:46Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [October 18, 2021, 9:39pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/21 "2021-10-18T21:39:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [October 18, 2021, 9:57pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/22 "2021-10-18T21:57:51Z")

</div>

Note that [&](https://docs.julialang.org/en/v1/base/math/#Base.:&) is a [bitwise and](https://en.wikipedia.org/wiki/Bitwise_operation#AND) operator. What is evaluated in `2 > 1 & 4 > 3` first is `1 & 4`:

```julia
julia> 1 & 4
0

```

But note the type of the result

```julia
julia> typeof(1 & 4)
Int64

```

What is evaluated subsequently is

```julia
julia> 2 > 0 > 3
false

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 18, 2021, 10:10pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/23 "2021-10-18T22:10:21Z")

</div>

> [@joa-quim](#):
>
> In Matlab it’s possible to have internal conditional code to **not compute** `b` if the function is called as `a = fm(...)` .

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:

> <https://github.com/GunnarFarneback/spatial_domain_toolbox/blob/5c55d5c4e57197cc9ccc3a6b9a1656dc57ae77dd/compute_displacement.m#L126-L130>

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.

> <https://github.com/GunnarFarneback/spatial_domain_toolbox/blob/5c55d5c4e57197cc9ccc3a6b9a1656dc57ae77dd/polyexp.m#L189-L212>

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

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [October 18, 2021, 10:28pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/24 "2021-10-18T22:28:04Z")

</div>

> [@leon](#):
>
> Function should allow us to define the outcome of the function like: `function [A, B] = f1(x, y, z)` , instead of requiring us to use `return` at the end of the function to return the results.

Oh no, let’s not go there!

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 18, 2021, 10:32pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/25 "2021-10-18T22:32:11Z")

</div>

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

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 18, 2021, 10:40pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/26 "2021-10-18T22:40:05Z")

</div>

> [@joa-quim](#):
>
> 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.

An analogous behavior (conditional computation of outputs) can be achieved in various ways:

1. Simply give the function different names for different numbers of outputs.
2. Return an object that computes the additional outputs as requested, for example by returning an iterator. e.g. Julia’s `lu(A)` function for LU factorization returns a factorization object `F`, which can either be used as-is to solve linear systems via `x = F \ b` or can be used to compute factors `F.L`, `F.U` as desired (which are otherwise not computed explicitly — this is accomplished by overloading `getproperty` in Julia), and which is also iterable so you can do `L, U = lu(a)` or `L, U = F`, for example.
3. In-place functions, conventionally written in Julia as `f!(x,y)`. If you don’t want a particular output, either don’t pass it — call `f!(x)` and dispatch to a different method — or pass some sentinel value (e.g. `nothing`).

Option 2 is the closest analogue of the Matlab `nargout`, and syntactically looks very similar to the user if an iterable object is returned.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [October 18, 2021, 11:08pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/27 "2021-10-18T23:08:36Z")

</div>

> [@leon](#):
>
> - Function should allow us to define the outcome of the function like: `function [A, B] = f1(x, y, z)` , instead of requiring us to use `return` at the end of the function to return the results.

I don’t really think this is that great of syntax, but I had a bit of fun making a macro to do this with a slight change and a bit of Julia flavour.

```julia
using MacroTools: @capture
using ExprTools: splitdef, combinedef

macro m(ex)
    @capture(ex, [args__] = fdef_) || error("Malformed input syntax")
    d = splitdef(fdef)
    d[:body] = :($(d[:body]); (; $(args...),))
    esc(combinedef(d))
end

```

Here it is in action:

```julia
julia> @m [m, s] = function stat(x)
           n = length(x);
           m = sum(x)/n;
           s = sqrt(sum((x .- m).^2/n));
       end
stat (generic function with 1 method)

julia> stat(rand(100))
(m = 0.46880423584214137, s = 0.2769434039119943)

```

One thing that’s kinda nice is that this returns the outputs as a `NamedTuple`, but if you prefer a regular `Tuple`, you could instead do

```julia
macro m(ex)
    @capture(ex, [args__] = fdef_) || error("Malformed input syntax")
    d = splitdef(fdef)
    d[:body] = :($(d[:body]); ($(args...),))
    esc(combinedef(d))
end

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [October 18, 2021, 11:16pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/28 "2021-10-18T23:16:15Z")

</div>

I guess the important thing to highlight is that Julia is such a wonderfully hackable language with very powerful metaprogramming, that it’s actually quite easy to mould the language syntax to your needs.

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [October 18, 2021, 11:19pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/29 "2021-10-18T23:19:14Z")

</div>

JuliaLang and @Mason are an impressive team.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [October 18, 2021, 11:28pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/30 "2021-10-18T23:28:39Z")

</div>

As of MATLAB 2020, there’s a keyword `arguments` which lets you define defaults, assertations etc. It’s pretty good (for MATLAB) but it’s nowhere near as nice as just doing this in the signature.

(And MATLAB 2021a introduced a `keyword=argument` syntax as a parallel to the existing (and awful) `'keyword', argument`)

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 19, 2021, 7:08am UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/31 "2021-10-19T07:08:05Z")

</div>

I meant if there were any differences besides the parenthesization.

---

<div class="post-metadata">

### Author: ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)
#### Post date: [October 19, 2021, 9:42am UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/32 "2021-10-19T09:42:26Z")

</div>

`&&` is a shortcuting operator, which means the second term isn’t evaluated if the first one is `false`, whereas `&` evaluates everything:

```julia-repl
julia> true && (println("a"); true)
a
true

julia> false && (println("a"); true)
false

julia> false & (println("a"); true)
a
false

```

~~the short-cutting behaviour doesn’t work for `.&&` though, it behaves like `.&` but with the nicer precedence:~~ for the short-circuit with `.&&`, there must be a broadcasted function, see [this answer](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/62) and the 2

next ones

```julia-repl
julia> [false] .&& [(println("a"); true)]
a
1-element BitVector:
 0

julia> [false, true] .&& println.(1:2)
2
2-element Vector{Union{Nothing, Bool}}:
 false
      nothing

```

`&&` can have non-booleans in the last term:

```julia-repl
julia> true && 2
2

julia> false && 2
false

julia> 2 && true
ERROR: TypeError: non-boolean (Int64) used in boolean context
Stacktrace:
 [1] top-level scope
   @ REPL[14]:1

```

while `&` is a bitwise operation:

```julia-repl
julia> 3 & true
1

julia> 2 & true
0

```

[https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation)

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 19, 2021, 9:43am UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/33 "2021-10-19T09:43:45Z")

</div>

> [@MarcMush](#):
>
> the short-cutting behaviour doesn’t work for `.&&` though, it behaves like `.&` but with the nicer precedence:
> 
> ```julia
> 
> ```

Ah ok. This is what I was asking. Thanks

---

<div class="post-metadata">

### Author: ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)
#### Post date: [October 19, 2021, 9:52am UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/34 "2021-10-19T09:52:49Z")

</div>

correction, it’s not really identical, this wouldn’t work with `&`:

```julia-repl
julia> rand(Bool, 5) .&& "abc"
5-element Vector{Any}:
 false
 false
      "abc"
      "abc"
 false

```

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 19, 2021, 10:06am UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/35 "2021-10-19T10:06:14Z")

</div>

Mmm. Are you sure it is not short-circuiting somehow?

Although I am not sure how “short-circuit” would work in broadcasting

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [October 19, 2021, 12:02pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/37 "2021-10-19T12:02:25Z")

</div>

> [@gustaphe](#):
>
> (And MATLAB 2021a introduced a `keyword=argument` syntax as a parallel to the existing (and awful) `'keyword', argument` )

Yeah, I think even MATLAB wants you to be using the Julia syntax for this now, which is why it was added.

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [October 19, 2021, 12:21pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/38 "2021-10-19T12:21:18Z")

</div>

Haha I remember writing a MatLab function to automatically generate the text of the most common `nargin` and `nargout` code based on a function signature (also in text), which I would then just paste into the top of my new function. Oh unhappy days.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [October 19, 2021, 1:02pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/39 "2021-10-19T13:02:22Z")

</div>

Hi, @leon! I just wanted to say hello and thank you for the suggestions and say that I hope the barrage of replies doesn’t feel overwhelming or unfriendly. People are just excited about programming languages here (especially Julia, of course), and being helpful. It seems like there are ways to do all these things, even if they’re not exactly like they are done in Matlab. Please don’t hesitate to ask about any other things that feel more awkward than they should—there may already be a way to express it better… or it may be an opportunity for us to improve the language and make something awkward a little easier, which is always exciting!

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [October 19, 2021, 1:46pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/40 "2021-10-19T13:46:31Z")

</div>

Many thanks for the very kind reply, Stefan! No worries, I find most of the replies to be very helpful. It’s also ok to push back on some of my suggestions. I now come to appreciate the Julia way of defining a function, for example.

As a newbie, I start to love Julia a lot. Therefore, I just hope Julia can be improved in certain ways.

I got lots of help from lots of people in this community recently and I’m wholeheartedly grateful. Thank you so much, Everyone!

---

<div class="post-metadata">

### Author: ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)
#### Post date: [October 19, 2021, 2:25pm UTC](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/41 "2021-10-19T14:25:29Z")

</div>

> [@PetrKryslUCSD](#):
>
> 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.

Just to play devil’s advocate, you can do essentially the same in Julia:

```julia
function f(x, y)

    A = stuff
    lines
    of
    code
    B = other stuff
    more
    code
    return (A, B)

end

```

But perhaps requiring the “return” statement (optional `return` keyword) encourages `A` and `B` to be computed close to or immediately before the return statement.

[Previous page](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987.md?page=1)

[Next page](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987.md?page=3)
