# How do I assign and branch?

**URL:** <https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 2, 2024, 3:37pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179 "2024-11-02T15:37:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [November 2, 2024, 3:37pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/1 "2024-11-02T15:37:55Z")

</div>

Here’s some code.

```julia
tmp = myFunction()
if !isnothing(tmp)
    doSomethingWith(tmp)
end

```

Some languages have a convenient way to express this. The disadvantage of what I have done is introduce a new temporary variable `tmp`, which serves no purpose other than as an optimization.

The optimization avoids calling `myFunction()` twice.

```julia
if !isnothing(myFunction())
    doSomethingWith(myFunction())
end

```

Is there a better way to express what I want to do here?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 2, 2024, 4:34pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/2 "2024-11-02T16:34:29Z")

</div>

You could define

```julia
dosomething(::Nothing) = nothing 
dosomething(val) = # do something 

```

And then call

```julia
dosomething(foo())

```

directly.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [November 2, 2024, 6:20pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/3 "2024-11-02T18:20:46Z")

</div>

Use a higher-order function:

```julia
function onsomething(f, val)
    if !isnothing(val)
       f(val)
    end
end

```

This can be used without naming the (`tmp`) argument

```julia
onsomething(doSomethingWith, foo())

```

or the do-syntax:

```julia
onsomething(foo()) do tmp # name it here
    doSomethingWith(tmp)
end

```

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [November 2, 2024, 6:37pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/4 "2024-11-02T18:37:49Z")

</div>

These are both really good suggestions, and not at all what I was imagining would be the solution. Thank you both for your comments

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 2, 2024, 7:01pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/5 "2024-11-02T19:01:06Z")

</div>

On some level, the logic for the convenient expression has to be handled separately, and that is usually put in a higher-order function and sometimes a macro.

To comment on the temporary variable, if your concern is less about having to write it but about it sticking around in a language where global variables don’t go away, you could either work inside a local scope or declare `local tmp` in an otherwise globally-scoped top-level expression (`begin`, `if`) to make local variables go away.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 2, 2024, 7:38pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/6 "2024-11-02T19:38:21Z")

</div>

You can do:

```Julia
julia> function foo()
           println("hello")
           return 1
       end
foo (generic function with 1 method)

julia> if (tmp = foo()) !== nothing
           sin(tmp)
       end
hello
0.8414709848078965

```

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [November 2, 2024, 9:34pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/7 "2024-11-02T21:34:55Z")

</div>

Adding currying to @bertschi’s suggestion is nice too.

```julia
onsomething(f) = x -> onsomething(f, x)

onsomething(doSomethingWith)(foo())

```

Has a similar flavor ask `skipmissing`!

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [November 4, 2024, 3:46pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/8 "2024-11-04T15:46:00Z")

</div>

To clarify what some people above have already hinted, there is no performance penalty to additional variables. The compiler will create a “variable” for the result of `myFunction()` whether you assign it to a named variable or not (unless it is eliminated as dead code).

```julia-repl
julia> code_lowered(function (a); b = a; c = b; d = c; return d*d; end, (Float64,))
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─ %1 = a
│ b = %1
│ %3 = b
│ c = %3
│ %5 = c
│ d = %5
│ %7 = d
│ %8 = d
│ %9 = %7 * %8
└── return %9
)

julia> code_llvm(function (a); b = a; c = b; d = c; return d*d; end, (Float64,); debuginfo=:none)
; Function Signature: var"#45"(Float64)
define double @"julia_#45_2173"(double %"a::Float64") #0 {
top:
  %0 = fmul double %"a::Float64", %"a::Float64"
  ret double %0
}

```

See that the `code_lowered` shows many assignments to temporary variables like `%1` (with more assignments than we even wrote, in fact). A named variable is just a value that has a name you can refer to it later, but the values are produced in any case.

But by the time we reach the `code_llvm`, all of those have vanished and it simply does the “equivalent” operation.

So if you are concerned about the performance cost of assigning a new variable, don’t be. If the namespace pollution bothers you, give the result a better name than `tmp` and it won’t feel so bad. If you really want the variable name gone anyway, then a `let` could do what you want (but has no _performance_ benefit in this situation):

```julia
let tmp = myFunction()
    # tmp only exists within this block
    if !isnothing(tmp)
        doSomethingWith(tmp)
    end
end

```

Note that, in some situations, the compiler _may_ be able to eliminate a second call to `myFunction()` and simply recycle the result of the first (the compiler must prove it will give the same result). But it’s better not to rely on that (preferring the `tmp = myFunction()` idiom) when you know you want it.

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [November 4, 2024, 3:51pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/9 "2024-11-04T15:51:19Z")

</div>

> [@mikmoore](#):
>
> If you really want the variable name gone anyway, then a `let` could do what you want (but has no _performance_ benefit in this situation):
> 
> ```julia
> let tmp = myFunction()
> # tmp only exists within this block
> if !isnothing(tmp)
> doSomethingWith(tmp)
> end
> end
> 
> ```

Thanks - another good solution

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [November 15, 2024, 3:00pm UTC](https://discourse.julialang.org/t/how-do-i-assign-and-branch/122179/10 "2024-11-15T15:00:45Z")

</div>

Some alternatives (similar to previous ones)

```julia
myFunction() |> x->isnothing(x) || doSomethingWith(x)
(tmp = myFunction()) === nothing || doSomethingWith(tmp) 

```
