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

I am getting an error with a type in my package: 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:

using GeoStats

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

Error message:

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?

I think you probably mean

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

instead of

    maxlag == nothing && (maxlag == 0.75maximum(lags))
1 Like

Oops! Thanks @quinnj, fixed it :slight_smile: