# Functions inside of struct block

**URL:** https://discourse.julialang.org/t/functions-inside-of-struct-block/13462
**Category:** General Usage
**Created:** [August 14, 2018, 8:11pm UTC](https://discourse.julialang.org/t/functions-inside-of-struct-block/13462 "2018-08-14T20:11:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)
#### Post date: [August 14, 2018, 8:11pm UTC](https://discourse.julialang.org/t/functions-inside-of-struct-block/13462/1 "2018-08-14T20:11:11Z")

</div>

I noticed this code in `Pkg`, and the the utility of the `copy` function inside the struct block eludes me even though it’s commented:

```julia
struct VersionSet
    intervals::Vector{VersionInterval}
    VersionSet(intervals::Vector{VersionInterval}) = new(normalize!(intervals))
    # copy is defined inside the struct block to call `new` directly
    # without going through `normalize!`
    Base.copy(vset::VersionSet) = new(copy(vset.intervals))
end

```

How is `copy` ever used? More generally, what is the scope of functions defined in the struct block?

---

<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 14, 2018, 8:26pm UTC](https://discourse.julialang.org/t/functions-inside-of-struct-block/13462/2 "2018-08-14T20:26:50Z")

</div>

It doesn’t define a new function so there isn’t really any scope to talk about (except the one of the original function).

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [August 14, 2018, 8:30pm UTC](https://discourse.julialang.org/t/functions-inside-of-struct-block/13462/3 "2018-08-14T20:30:15Z")

</div>

To clarify Kristoffer’s comment, that line of code extends the same `Base.copy` as if it were defined outside the `struct`. The reason it is defined _inside_ is that it calls `new`, which is only accessible inside the scope of the `struct` definition.

The “function” `new` is ever only accessible from inside a `struct` definition, and directly constructs an instance of `VersionSet`, avoiding the constructor (which is defined here on the third line). That constructor does some work to preserve an invariant across all instances of `VersionSet`, but since the purpose of `copy` is to duplicate an existing pre-constructed `VersionSet`, we know that work can be avoided.

With a struct like this, where the inner constructor always does some work, the only way forward is to call `new` directly, which you can only do from inside the scope of a `struct` definition, which is why the extension of `Base.copy` ended up being put there – to have access to `new`.

---

<div class="post-metadata">

### Author: ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)
#### Post date: [August 14, 2018, 9:14pm UTC](https://discourse.julialang.org/t/functions-inside-of-struct-block/13462/4 "2018-08-14T21:14:28Z")

</div>

Ah I see now, thanks for the great explanation!
