Shortcut if isnothing(variable = match(...))

Hi all,
does it exist a shorter syntax to write some like this:

elseif !isnothing(m = match(...)
   .. use m

rather than the more verbose and nested block:

  m = match(...)
  if !isnothing(m)
    .. use m

You are looking for something. See ? something for details.

Since 1.7 there is also a macro @something which makes slightly easier syntax (and is also faster, since it short-circuits)

julia> x = "hello";

julia> match(r"zz", x)

julia> @something match(r"zz", x) "tt"
1 Like

I usually use this style:

elseif (m = match(...); m !== nothing)
    # use m...

You can just use

x = 1
if (y = x) !== nothing
 @show y

The difference is that !== is not a function call, so it doesn’t get parsed as an argument to a function, but just an assignment to a variable in the scope of if.

1 Like