# "LoadError: invalid redefinition of constant" when creating new Distribution

**URL:** https://discourse.julialang.org/t/loaderror-invalid-redefinition-of-constant-when-creating-new-distribution/34786
**Category:** General Usage
**Created:** [February 18, 2020, 3:45am UTC](https://discourse.julialang.org/t/loaderror-invalid-redefinition-of-constant-when-creating-new-distribution/34786 "2020-02-18T03:45:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Arrigo\_Benedetti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arrigo_benedetti/32/25545_2.png) [@Arrigo\_Benedetti](https://discourse.julialang.org/u/Arrigo_Benedetti)
#### Post date: [February 18, 2020, 3:45am UTC](https://discourse.julialang.org/t/loaderror-invalid-redefinition-of-constant-when-creating-new-distribution/34786/1 "2020-02-18T03:45:48Z")

</div>

I am trying to create a new distribution (bivariate Projected Normal) using skellam.jl as an example, however I am getting this error:

```julia
push!(LOAD_PATH, ".")

using Revise
using ProjectedNormal

┌ Info: Precompiling ProjectedNormal [top-level]
└ @ Base loading.jl:1273
ERROR: LoadError: invalid redefinition of constant ProjectedNormal

```

This is the code in the module:

```julia
module ProjectedNormal

using Distributions
using SpecialFunctions

struct ProjectedNormal{T<:Real} <: ContinuousUnivariateDistribution
	μ::T
	ξ::T
	
    function ProjectedNormal{T}(μ::T, ξ::T) where {T <: Real}
        return new{T}(μ, ξ)
    end
end

#### Parameters
params(d::ProjectedNormal) = (d.μ, d.ξ)
partype(::ProjectedNormal{T}) where {T} = T

function pdf(d::ProjectedNormal, θ::Real)
    μ, ξ = params(d)
    1/2π*exp(-ξ^2/2*(1+sqrt(π/2)*ξ*cos(θ-μ)*exp(ξ^2*cos(θ-μ)^2/2)*(1+erf(ξ*cos(θ-μ)/sqrt(2)))))	
end

#### Sampling
function rand(d::ProjectedNormal)
	μ, ξ = params(d)
	atan(cos(μ)+randn(),sin(μ)+randn())
end

function rand(rng::AbstractRNG, d::ProjectedNormal)
	μ, ξ = params(d)
	atan(cos(μ)+randn(rng),sin(μ)+randn(rng))
end

end

```

Any ideas? Thanks!

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [February 18, 2020, 4:31am UTC](https://discourse.julialang.org/t/loaderror-invalid-redefinition-of-constant-when-creating-new-distribution/34786/2 "2020-02-18T04:31:04Z")

</div>

It looks like the module name is available as a global variable within the module, so when you define your `ProjectedNormal` struct, it’s trying to redefine the constant module reference. Here’s a minimal example of this problem:

```julia
julia> module A

       struct A end

       end
ERROR: invalid redefinition of constant A

```

By the way, this is unrelated to your question, but you can leverage multiple dispatch to reduce the code duplication in your `rand` methods:

```julia
using Random

function rand(d::ProjectedNormal)
	rand(Random.GLOBAL_RNG, d)
end

function rand(rng::AbstractRNG, d::ProjectedNormal)
	μ, ξ = params(d)
	atan(cos(μ)+randn(rng),sin(μ)+randn(rng))
end

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [February 18, 2020, 5:27am UTC](https://discourse.julialang.org/t/loaderror-invalid-redefinition-of-constant-when-creating-new-distribution/34786/3 "2020-02-18T05:27:01Z")

</div>

Just to add, the usual solution is to pluralize the module, e.g. ProjectedNormals in this case.

---

<div class="post-metadata">

### Author: ![Arrigo\_Benedetti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arrigo_benedetti/32/25545_2.png) [@Arrigo\_Benedetti](https://discourse.julialang.org/u/Arrigo_Benedetti)
#### Post date: [February 18, 2020, 9:51pm UTC](https://discourse.julialang.org/t/loaderror-invalid-redefinition-of-constant-when-creating-new-distribution/34786/4 "2020-02-18T21:51:24Z")

</div>

Yes, that fixed it. Thanks!
