# Constructor with optional argument of type Union{T,Void}

**URL:** <https://discourse.julialang.org/t/constructor-with-optional-argument-of-type-union-t-void/4693>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 6, 2017, 4:36pm UTC](https://discourse.julialang.org/t/constructor-with-optional-argument-of-type-union-t-void/4693 "2017-07-06T16:36:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 6, 2017, 4:36pm UTC](https://discourse.julialang.org/t/constructor-with-optional-argument-of-type-union-t-void/4693/1 "2017-07-06T16:36:19Z")

</div>

I am getting an error with a type in my package: [https://github.com/juliohm/GeoStats.jl/blob/master/src/empirical\_variograms.jl](https://github.com/juliohm/GeoStats.jl/blob/master/src/empirical_variograms.jl)

There I define an optional argument `maxlag` for the constructor. When I omit the argument, I get an error as if `maxlag` were of type `Void`:

```julia
using GeoStats

EmpiricalVariogram(eye(3), ones(3)) # fails
EmpiricalVariogram(eye(3), ones(3), maxlag=2.) # works

```

Error message:

```julia
ERROR: MethodError: no method matching /(::Void, ::Int64)
Closest candidates are:
  /(::BigInt, ::Union{Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8}) at gmp.jl:380
  /(::BigFloat, ::Union{Int16, Int32, Int64, Int8}) at mpfr.jl:339
  /(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, ::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) at int.jl:38
  ...
Stacktrace:
 [1] GeoStats.EmpiricalVariogram{Float64,Float64,GeoStats.EuclideanDistance}(::Array{Float64,2}, ::Array{Float64,1}, ::Int64, ::Void, ::GeoStats.EuclideanDistance) at /home/juliohm/.julia/v0.6/GeoStats/src/empirical_variograms.jl:67
 [2] GeoStats.EmpiricalVariogram(::Array{Float64,2}, ::Array{Float64,1}) at /home/juliohm/.julia/v0.6/GeoStats/src/empirical_variograms.jl:81

```

You can see in the code that I set `maxlag` to a number if it is `nothing`.

What is the cause of the issue? Do you have suggestions on how to improve the code?

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [July 6, 2017, 4:43pm UTC](https://discourse.julialang.org/t/constructor-with-optional-argument-of-type-union-t-void/4693/2 "2017-07-06T16:43:40Z")

</div>

I think you probably mean

```julia
    maxlag == nothing && (maxlag = 0.75maximum(lags))

```

instead of

```julia
    maxlag == nothing && (maxlag == 0.75maximum(lags))

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 6, 2017, 4:50pm UTC](https://discourse.julialang.org/t/constructor-with-optional-argument-of-type-union-t-void/4693/3 "2017-07-06T16:50:35Z")

</div>

Oops! Thanks @quinnj, fixed it 🙂
