# Overloading struct and related methods

**URL:** <https://discourse.julialang.org/t/overloading-struct-and-related-methods/8285>\
**Category:** General Usage\
**Created:** [January 11, 2018, 9:26am UTC](https://discourse.julialang.org/t/overloading-struct-and-related-methods/8285 "2018-01-11T09:26:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver\_Lylloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver_lylloff/32/9022_2.png) [@Oliver\_Lylloff](https://discourse.julialang.org/u/Oliver_Lylloff)\
**Post date:** [January 11, 2018, 9:26am UTC](https://discourse.julialang.org/t/overloading-struct-and-related-methods/8285/1 "2018-01-11T09:26:08Z")

</div>

Hi there,

I want to use a struct and related methods from the package [ProximalOperators.jl](https://github.com/kul-forbes/ProximalOperators.jl). Specifically, I want to change the field `lambda` of an immutable struct.

MWE:

```julia
using ProximalOperators

function foo(g::T) where T <: ProximableFunction
    g.lambda = 1.0
end

foo(NormL1())

```

which gives an `ERROR: type is immutable`. Makes sense, since [NormL1](https://github.com/kul-forbes/ProximalOperators.jl/blob/07df4b3b391d5dac71cfde61470e8919f090baec/src/functions/normL1.jl#L20-L32) is immutable…

How do I proceed from here?

I tried changing the struct `NormL1` from immutable to mutable inside my own module:

```julia
module mymodule
import ProximalOperators: NormL1, ProximableFunction
export NormL1

mutable struct NormL1{T <: Union{Real, AbstractArray}} <: ProximableFunction
  lambda::T
  function NormL1{T}(lambda::T) where {T <: Union{Real, AbstractArray}}
    if !(eltype(lambda) <: Real)
      error("λ must be real")
    end
    if any(lambda .< 0)
      error("λ must be nonnegative")
    else
      new(lambda)
    end
  end
end

NormL1{R <: Real}(lambda::R=1.0) = NormL1{R}(lambda)

end

```

Now I can do `foo(mymodule.NormL1())` without errors. However, I cannot see how to overload all existing methods in ProximalOperators that make use of `NormL1`, see e.g., [prox!](https://github.com/kul-forbes/ProximalOperators.jl/blob/07df4b3b391d5dac71cfde61470e8919f090baec/src/functions/normL1.jl#L62-L136), without copy-pasting all of them to `mymodule` and changing the relevant function signatures from `ProximalOperators.NormL1` to `mymodule.NormL1`.

Links and tips for handling this, would be very much appreciated.

Best,  
Oliver

---

<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:** [January 11, 2018, 9:49am UTC](https://discourse.julialang.org/t/overloading-struct-and-related-methods/8285/2 "2018-01-11T09:49:06Z")

</div>

You can’t redefine a type. Also, unless it is exported, your definition inside another module (possibly `Main`) will be different from the one in the original module, since they are different namespaces.

Perhaps you should simply create a new `NormL1` instance with the desired value? It [seems to be](https://github.com/kul-forbes/ProximalOperators.jl/blob/02f78524665732714b8a88fd9dbd02400f36a5d3/src/functions/normL1.jl#L20) just a wrapper with some check, should not be too costly.

---

<div class="post-metadata">

**Author:** ![Oliver\_Lylloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver_lylloff/32/9022_2.png) [@Oliver\_Lylloff](https://discourse.julialang.org/u/Oliver_Lylloff)\
**Post date:** [January 11, 2018, 9:54am UTC](https://discourse.julialang.org/t/overloading-struct-and-related-methods/8285/3 "2018-01-11T09:54:03Z")

</div>

Thanks,  
I agree that it is not too costly for this MWE, but if I wanted more methods and related functions from that package, I would end up with a lot of redundant code, right?

Furthermore, I actually want to iterate over different lambda, not just the 1.0 I set above…

---

<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:** [January 11, 2018, 9:57am UTC](https://discourse.julialang.org/t/overloading-struct-and-related-methods/8285/4 "2018-01-11T09:57:00Z")

</div>

> [@Oliver\_Lylloff](#):
>
> I would end up with a lot of redundant code, right?

Perhaps you misunderstand my suggestion, I was not recommending that you duplicate any code, just create new **instances** , eg `NormL1(1.0)`, since you cannot change existing ones.

It is possible that you are not using this package as it was intended (I cannot say as I am not familiar with it). It appears to be superbly documented, so if something is not clear after reading the docs, perhaps ask the author.

---

<div class="post-metadata">

**Author:** ![anon61610682](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon61610682](https://discourse.julialang.org/u/anon61610682)\
**Post date:** [January 11, 2018, 10:00am UTC](https://discourse.julialang.org/t/overloading-struct-and-related-methods/8285/5 "2018-01-11T10:00:30Z")

</div>

> [@Oliver\_Lylloff](#):
>
> Furthermore, I actually want to iterate over different lambda, not just the 1.0 I set above…

Why not create different instances of `NormL1` inside your loop as in:

```julia
using ProximalOperators

function foo(g::T) where T <: ProximableFunction
    # g.lambda = 1.0
    # simply use g
end

for lambda in linspace(0, 10, 100)
  foo(NormL1(lambda))
end

```

In the end, `NormL1` is constructed either via a scalar or some array. Arrays are passed by pointer, and neither of the constructions should result in a huge penalty in performance.

---

<div class="post-metadata">

**Author:** ![Oliver\_Lylloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver_lylloff/32/9022_2.png) [@Oliver\_Lylloff](https://discourse.julialang.org/u/Oliver_Lylloff)\
**Post date:** [January 11, 2018, 10:36am UTC](https://discourse.julialang.org/t/overloading-struct-and-related-methods/8285/6 "2018-01-11T10:36:11Z")

</div>

Thanks @Tamas_Papp and @aytekinar. That’s exactly what I needed.
