# A switch type function

**URL:** https://discourse.julialang.org/t/a-switch-type-function/13491
**Category:** New to Julia
**Tags:** control-flow
**Created:** [August 15, 2018, 8:45am UTC](https://discourse.julialang.org/t/a-switch-type-function/13491 "2018-08-15T08:45:57Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)
#### Post date: [August 15, 2018, 8:45am UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/1 "2018-08-15T08:45:57Z")

</div>

It seems that Julia doesn’t support a switch-case type function. I have the following code example, and it wouldn’t pass in Julia:

Function Gauss(option)  
##Here, option is a string  
if option ==‘L3’  
a=0  
end

end

What is the solution for this?

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [August 15, 2018, 8:51am UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/2 "2018-08-15T08:51:01Z")

</div>

I was looking for this too, but it is currently not implemented. See the following proposal for more information:

> [A case/switch statement for Julia · Issue #18285 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/18285)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 15, 2018, 8:55am UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/3 "2018-08-15T08:55:57Z")

</div>

> [@FiniteelementDuan](#):
>
> and it wouldn’t pass in Julia:
> 
> ```julia
> Function Gauss(option)
> ##Here, option is a string
> if option ==‘L3’
> a=0
> end
> 
> end
> 
> ```
> 
> What is the solution for this?

Using valid syntax is always an option:

```julia
function gauss(option)
    if option == "L3"
        a=0
    end
end

```

---

<div class="post-metadata">

### Author: ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)
#### Post date: [August 15, 2018, 8:57am UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/4 "2018-08-15T08:57:05Z")

</div>

Thanks for the link. There are no alternatives for this? If you look the code example above, it looks very simple to me.

---

<div class="post-metadata">

### Author: ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)
#### Post date: [August 15, 2018, 9:00am UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/5 "2018-08-15T09:00:01Z")

</div>

Ok. Many thanks. It is working!

---

<div class="post-metadata">

### Author: ![Olof\_Salberger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olof_salberger/32/4850_2.png) [@Olof\_Salberger](https://discourse.julialang.org/u/Olof_Salberger)
#### Post date: [August 15, 2018, 12:26pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/6 "2018-08-15T12:26:22Z")

</div>

Python doesn’t have one either. You’re expected to use multiple dispatch, elseif chains, or dictionaries for most cases.

The proposal for a switch statement is not intended to be something you use all the time, but for performance sensitive code where you want to guarentee that the compiler is generating a jump table.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 15, 2018, 12:29pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/7 "2018-08-15T12:29:25Z")

</div>

A chain of simple `if` statements generate a branch table in julia anyway.

---

<div class="post-metadata">

### Author: ![Olof\_Salberger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olof_salberger/32/4850_2.png) [@Olof\_Salberger](https://discourse.julialang.org/u/Olof_Salberger)
#### Post date: [August 15, 2018, 12:32pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/8 "2018-08-15T12:32:02Z")

</div>

Right, optimization passes largely eliminate the direct need for it. Syntax-wise, Julia has macros, and there’s packages like [match.jl](http://kmsquire.github.io/Match.jl/latest/) that provide macros for this and more.

---

<div class="post-metadata">

### Author: ![ameresv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ameresv/32/14372_2.png) [@ameresv](https://discourse.julialang.org/u/ameresv)
#### Post date: [April 26, 2020, 6:35pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/9 "2020-04-26T18:35:26Z")

</div>

Hi! I have tested your suggestion, however, I encountered a problem for the case that I am evaluating:  
I want to do something like this:

```julia
function tipo2(n)
    if 0.1 ≤ n ≤ 0.6
        m = 1
    elseif 0.6 < n ≤ 0.8
        m = 1.1
    elseif 0.8 < n ≤ 1.0
        m = 1.2
    elseif 1.0 < n ≤ 1.3
        m = 1.35
    else m = 2.2
    end
end

```

Using @match I tried to use like this:

```julia
m = @match n begin
        0.1 ≤ n ≤ 0.6 => 1
        0.6 < n ≤ 0.8 => 1.1
        0.8 < n ≤ 1.0 => 1.2
        1.0 < n ≤ 1.3 => 1.35
        _ => 2.2
    end

```

However, no matter what will be the value of n (which varies from 0.1 to 1.6 by 0.01), m always is 2.2.  
Did I do something wrong?  
Regards

---

<div class="post-metadata">

### Author: ![Olof\_Salberger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olof_salberger/32/4850_2.png) [@Olof\_Salberger](https://discourse.julialang.org/u/Olof_Salberger)
#### Post date: [August 9, 2020, 9:47am UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/10 "2020-08-09T09:47:16Z")

</div>

You’ll probably want to use range patterns instead of inequalities here if you are using @match.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 9, 2021, 6:45pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/11 "2021-08-09T18:45:12Z")

</div>

@Olof_Salberger, how could one use the range patterns in [Match.jl](https://kmsquire.github.io/Match.jl/latest/) in order to solve @ameresv’s switch problem?

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [August 9, 2021, 6:56pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/12 "2021-08-09T18:56:13Z")

</div>

You can use MLStyle.jl for this [Pattern Matching — MLStyle.jl Documentation](https://thautwarm.github.io/MLStyle.jl/latest/syntax/pattern.html#guards)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 9, 2021, 7:04pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/13 "2021-08-09T19:04:28Z")

</div>

@jzr, thanks a lot, it works but a bit more verbose:

```julia
using MLStyle
n = 1.0
m = @match n begin
        if 0.1 ≤ n ≤ 0.6 end => 1
        if 0.6 < n ≤ 0.8 end => 1.1
        if 0.8 < n ≤ 1.0 end => 1.2
        if 1.0 < n ≤ 1.3 end => 1.35
        _ => 2.2
    end

1.2

```

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [August 9, 2021, 7:13pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/14 "2021-08-09T19:13:48Z")

</div>

Yes it would be nice to have a shorter syntax than `if`/`end`. `where` would be a good candidate but it currently has the wrong operator precedence.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [August 9, 2021, 8:13pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/15 "2021-08-09T20:13:09Z")

</div>

Less verbose (and builtin) is chained ternaries. With indentation they can be quite readable for short expressions like these:

```julia
n = 1.0
m = n ≤ 0.1 ? 0.0 :
    0.1 < n ≤ 0.6 ? 1.0 :
    0.6 < n ≤ 0.8 ? 1.1 :
    0.8 < n ≤ 1.0 ? 1.2 :
    1.0 < n ≤ 1.3 ? 1.35 :
    #= otherwise =# 2.2

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 9, 2021, 8:20pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/16 "2021-08-09T20:20:50Z")

</div>

@mbauman, love it.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [August 13, 2021, 4:19pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/17 "2021-08-13T16:19:58Z")

</div>

Using an early return is quite common in Julia, it’s concise and readable too:

```julia
function tipo2(n)
    0.1 ≤ n ≤ 0.6 && return 1.0
    0.6 < n ≤ 0.8 && return 1.1
    0.8 < n ≤ 1.0 && return 1.2
    1.0 < n ≤ 1.3 && return 1.35
    return 2.2
end

```

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [August 13, 2021, 5:44pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/18 "2021-08-13T17:44:45Z")

</div>

Magic `switch` function 😜:

```julia
switch(f, x...) = f(x...)

```

Usage:

```julia
n = 1.0
m = switch(n) do n
    n ≤ 0.1 && return 0.0
    n ≤ 0.6 && return 1.0
    n ≤ 0.8 && return 1.1
    n ≤ 1.0 && return 1.2
    n ≤ 1.3 && return 1.35
    return 2.2
end

```

```julia
1.2

```

```julia
x, y = 0.3, 0.4+eps()
z = switch(x, y) do x, y
    x^2 + y^2 ≤ 0.5^2 && return 0.5
    x^2 + y^2 ≤ 1.0^2 && return 1.0
    return 2.0
end

```

```julia
1.0

```

Edit 1: Thank you, Eben60-san.  
Edit 2: Add another example.

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [August 13, 2021, 8:16pm UTC](https://discourse.julialang.org/t/a-switch-type-function/13491/19 "2021-08-13T20:16:13Z")

</div>

It is not necessary to check the left interval border.

```julia
n = 1.1
m = n ≤ 0.1 ? 0.0 :
    n ≤ 0.6 ? 1.0 :
    n ≤ 0.8 ? 1.1 :
    n ≤ 1.0 ? 1.2 :
    n ≤ 1.3 ? 1.35 :
    #= otherwise =# 2.2

```
