# I don't understand this syntax. Documentation request

**URL:** <https://discourse.julialang.org/t/i-dont-understand-this-syntax-documentation-request/85837>\
**Category:** New to Julia\
**Created:** [August 16, 2022, 4:30pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-syntax-documentation-request/85837 "2022-08-16T16:30:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingoslo](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@kingoslo](https://discourse.julialang.org/u/kingoslo)\
**Post date:** [August 16, 2022, 4:30pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-syntax-documentation-request/85837/1 "2022-08-16T16:30:18Z")

</div>

Hello,

I am cooperating with someone on a Julia package, and I have barely worked in Julia before. I can read the majority of the stuff in the package, but sometimes I get stuck on some syntax that is so different from what I have seen in other languages, that I cannot read it. And sometimes I cannot find documentation in a reasonable time. Here is an example:

`isoff(data::Data{T}) where {T} = (T == Tuple{Int64,Int64} || T == Vector{Tuple{Int64,Int64}})`

I know what Unionall types are, but I don’t understand any of the following

1. What is `isoff`?
2. If `where` defines a unionall type, why is there assignment ( `=` ) on this line? Or perhaps it isn’t an assignment?
3. How can you have equality comparison on the right-hand side when it seems you are declaring some unionall type under a function `isoff`?
4. Why does `Tuple` have two arguments and not exactly one?
5. Why would you do boolean or on the right-hand side?
6. What is the meaning of this line of code?

Any help would be appreciated.

Kind regards from Marius

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 16, 2022, 4:37pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-syntax-documentation-request/85837/2 "2022-08-16T16:37:22Z")

</div>

As far as I can tell this is a one line function defintion defining a function `isoff` which takes in an argument `data` which is a parametric type and then check whether the type parameter of that particular instance is either a `Tuple{Int64, Int64}` or a vector of `Tuple{Int64, Int64}`. Example:

```julia
julia> struct Data{T}
          x::T
       end

julia> x = Data((1, 2))
Data{Tuple{Int64, Int64}}((1, 2))

julia> isoff(data::Data{T}) where {T} = (T == Tuple{Int64,Int64} || T == Vector{Tuple{Int64,Int64}})
isoff (generic function with 1 method)

julia> isoff(x)
true

julia> x2 = Data([(1, 2), (3, 4)])
Data{Vector{Tuple{Int64, Int64}}}([(1, 2), (3, 4)])

julia> isoff(x2)
true

julia> x3 = Data("a")
Data{String}("a")

julia> isoff(x3)
false

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 16, 2022, 4:48pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-syntax-documentation-request/85837/3 "2022-08-16T16:48:59Z")

</div>

A function can be defined in the standard way:

```julia
function foo(x, y)
    return x + y
end

```

or in the one-line short form way

```julia
foo(x, y) = x + y

```

The short form syntax is for simple functions that can become easier to write and read when used correctly

This:

> [@kingoslo](#):
>
> ```julia
> isoff(data::Data{T}) where {T} = (T == Tuple{Int64,Int64} || T == Vector{Tuple{Int64,Int64}})
> 
> ```

is, IMHO, a misuse of the short form syntax. It makes the code almost unreadable, especially for those who are not used to it. It is much better to write:

```julia
function isoff(data::Data{T}) where {T}
    return (T == Tuple{Int64,Int64}) || (T == Vector{Tuple{Int64,Int64}})
end

```

Short form syntax should only be used when it improves readability, in the above example it does not.

> [@kingoslo](#):
>
> Why does `Tuple` have two arguments and not exactly one?

They are not arguments, but type parameters:

```julia
julia> typeof((10, 231))
Tuple{Int64, Int64}

```

It’s a type of a tuple of two `Int64`s.

```julia
julia> typeof((10, 231, -5))
Tuple{Int64, Int64, Int64}

julia> typeof((10, "Hello", 5.0))
Tuple{Int64, String, Float64}

```

etc.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 16, 2022, 4:51pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-syntax-documentation-request/85837/4 "2022-08-16T16:51:34Z")

</div>

> - What is `isoff`?

`isoff` is the function that’s being defined here. This line is basically equivalent to:

```julia
function isoff(data::Data{T}) where {T}
  return (T == Tuple{Int64, Int64} || T == Vector{Tuple{Int64,Int64}})
end

```

> - If `where` defines a unionall type, why is there assignment ( `=` ) on this line? Or perhaps it isn’t an assignment?

This is the short form a function definition, which uses the assignment syntax. The placement of the `where` sometimes throws people off, but it should be read as `isoff(...) = ...` i.e. the assignment is to the method signature, not to `{T}`.

> - How can you have equality comparison on the right-hand side when it seems you are declaring some unionall type under a function `isoff`?

I’m not sure what you mean here, but hopefully the previous explanation clears this one up too. The `where {T}` syntax is being used so that we can then check whether `T` belongs to one of these two types. The result of that check is the return value of the method.

> - Why does `Tuple` have two arguments and not exactly one?

`Tuple`s are parametrised by the type of each element of the tuple, so `Tuple{Int64, Int64}` says that it’s a two-tuple with both elements being `Int64`s. This can also be written as `NTuple{2, Int64}`.

> - Why would you do boolean or on the right-hand side?

The right hand side is the return value of the function, and `isoff` is apparently true if `T` matches either of those types.

> - What is the meaning of this line of code?

Hope that’s clear from the above!

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 16, 2022, 4:56pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-syntax-documentation-request/85837/5 "2022-08-16T16:56:49Z")

</div>

A more Julian way of writing this function would be to use dispatch:

```julia
julia> isoff(data::Data{NTuple{2, Int64}}) = true
isoff (generic function with 1 method)

julia> isoff(data::Data{Vector{NTuple{2, Int64}}}) = true
isoff (generic function with 2 methods)

julia> isoff(data::Data) = false
isoff (generic function with 3 methods)

julia> x = Data((1, 2));

julia> isoff(x)
true

julia> x2 = Data([(1, 2), (3, 4)]);

julia> isoff(x2)
true

julia> x3 = Data("a");

julia> isoff(x3)
false

```

---

<div class="post-metadata">

**Author:** ![kingoslo](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@kingoslo](https://discourse.julialang.org/u/kingoslo)\
**Post date:** [August 16, 2022, 5:10pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-syntax-documentation-request/85837/6 "2022-08-16T17:10:28Z")

</div>

I get it now. You three were great.

Thanks for your time.
