# "where" syntax

**URL:** <https://discourse.julialang.org/t/where-syntax/5876>\
**Category:** General Usage\
**Created:** [September 13, 2017, 11:05pm UTC](https://discourse.julialang.org/t/where-syntax/5876 "2017-09-13T23:05:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [September 13, 2017, 11:05pm UTC](https://discourse.julialang.org/t/where-syntax/5876/1 "2017-09-13T23:05:13Z")

</div>

I’m trying to update the following code (taken from an [Andreas Noack talk](http://andreasnoack.github.io/talks/2015AprilStanford_AndreasNoack.ipynb)) from to julia-0.5 to julia-0.6. The GF{P,T} type represents a finite Galois field, essentially the integers modulo P where P is prime.

```julia
# Scalar finite fields. P is the modulus, T is the integer type (Int16, Int32, ...)
immutable GF{P,T<:Integer} <: Number
    data::T
    function GF(x::Integer)
        return new(mod(x, P))
    end
end

```

julia-0.6 gives the warning

```julia
WARNING: deprecated syntax "inner constructor GF(...) around REPL[6]:5".
Use "GF{P,T}(...) where {P,T}" instead.

```

What is the correct revision of the `immutable GF` type declaration? I’ve read documentation and have tried a number of things, but Im afraid I can’t figure it out.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [September 14, 2017, 12:05am UTC](https://discourse.julialang.org/t/where-syntax/5876/2 "2017-09-14T00:05:36Z")

</div>

```julia
julia> struct GF{P,T<:Integer} <: Number
           data::T
           function GF{P,T}(x::Integer) where {P, T}
               return new(mod(x, P))
           end
       end

```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [September 14, 2017, 12:07am UTC](https://discourse.julialang.org/t/where-syntax/5876/3 "2017-09-14T00:07:02Z")

</div>

As suggested by the warning, you should do:

```julia
struct GF{P,T<:Integer} <: Number
    data::T
    GF{P,T}(x::T) where {P,T<:Integer} = new(mod(x, P))
end

```

You can also add an outer constructor

```julia
GF{P}(x::T) where {P,T<:Integer} = GF{P,T}(x)

```

to avoid needing to call `GF{6,Int}(7)`, and instead only specify the modulus: `GF{6}(7)`

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [September 14, 2017, 12:39am UTC](https://discourse.julialang.org/t/where-syntax/5876/4 "2017-09-14T00:39:53Z")

</div>

@cstjean & @jebej, thank you!

Canyou explain the difference between `GF{P,T}(x::Integer) where {P, T}` and `GF{P,T}(x::T) where {P,T<:Integer}` in your answers? Both work, but I don’t understand what the first accomplishes.

@jebej your outer constructer prevents an infinite loop that otherwise occurs during instantiation of `GF{9}(5)`, which was the next item on my agenda. Thanks for that, too.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [September 14, 2017, 12:45am UTC](https://discourse.julialang.org/t/where-syntax/5876/5 "2017-09-14T00:45:31Z")

</div>

Note that with the new syntax, you can now also do

```julia
julia> struct GF{P,T<:Integer} <: Number
           data::T
           function GF{P}(x::Integer) where P
               data = mod(x, P)
               new{P, typeof(data)}(data)
           end
       end

julia> GF{4}(15)
GF{4,Int64}(3)

```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [September 14, 2017, 12:47am UTC](https://discourse.julialang.org/t/where-syntax/5876/6 "2017-09-14T00:47:25Z")

</div>

> [@John\_Gibson](#):
>
> Canyou explain the difference between GF{P,T}(x::Integer) where {P, T} and GF{P,T}(x::T) where {P,T\<:Integer} in your answers? Both work, but I don’t understand what the first accomplishes.

In that case I believe the two methods will be identical. I wrote it that way to be explicit that the parameter `T` will be the type of `x`.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [September 14, 2017, 12:54am UTC](https://discourse.julialang.org/t/where-syntax/5876/7 "2017-09-14T00:54:38Z")

</div>

They won’t necessarily do the same thing if `typeof(x)` is not the same as `T`. The syntax `GF{P,T}(x::Integer)` allows you to specify the type of the `data` field of `GF` directly, independent of the type of `x` (but still constrains `x` to be an `Integer`, of course). So it allows `GF{4,Int64}(Int32(4))`, for example.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [September 14, 2017, 6:25am UTC](https://discourse.julialang.org/t/where-syntax/5876/8 "2017-09-14T06:25:42Z")

</div>

I don’t understand the purpose of `where {P,T}` below. What information is that providing to the compiler?

> [@cstjean](#):
>
> ```julia
> julia> struct GF{P,T<:Integer} <: Number
> data::T
> function GF{P,T}(x::Integer) where {P, T}
> return new(mod(x, P))
> end
> end
> 
> ```

I can better understand the point of `where` in @jebej’s ` GF{P,T}(x::T) where {P,T<:Integer} = new(mod(x, P))`. It’s specifying that this function aapplies only when `T` is a subtype of `Integer`. But in the other case, it looks like the compiler is requiring me to provide a constraint that is empty.

I clearly don’t understand the `where` keyword. It is brought up incidentally in the Types and Methods pages of [https://docs.julialang.org](https://docs.julialang.org), but I don’t find an explanation anywhere of its purpose with simple explanatory examples of its main uses. Where should I look for that? An issue on Julia github?

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [September 14, 2017, 7:40am UTC](https://discourse.julialang.org/t/where-syntax/5876/9 "2017-09-14T07:40:41Z")

</div>

Stefan has a really excellent answer justifying the new where syntax on stackoverflow.

> <https://stackoverflow.com/questions/41931289/where-in-function-definitions-in-julia-0-6/41940710#41940710>

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [September 14, 2017, 10:55am UTC](https://discourse.julialang.org/t/where-syntax/5876/10 "2017-09-14T10:55:35Z")

</div>

The stackoverflow discussion lalso ead me to this [where syntax thread](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/14) on Discourse from July --which I would have found had I searched “where syntax” Discourse instead of Google in the first place. Thanks everyone for the help.
