Arguments, unexpected behavior

https://docs.julialang.org/en/v1/manual/functions/#Optional-Arguments-1

Julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.7.0 (2018-08-08 06:46 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-w64-mingw32

julia> function Date(y::Int64, m::Int64=1, d::Int64=1)
           err = validargs(Date, y, m, d)
               err === nothing || throw(err)
                   return Date(UTD(totaldays(y, m, d)))
                   end
Date (generic function with 3 methods)

julia> Date(2000, 12, 12)
ERROR: UndefVarError: validargs not defined
Stacktrace:
 [1] Date(::Int64, ::Int64, ::Int64) at .\REPL[1]:2
 [2] top-level scope at none:0
julia> function Re_Date(y::Int64, m::Int64=1, d::Int64=1)
           err = validargs(Re_Date, y, m, d)
               err === nothing || throw(err)
                   return Re_Date(UTD(totaldays(y, m, d)))
                   end
Re_Date (generic function with 3 methods)
julia> Re_Date(2000, 12, 12)
ERROR: UndefVarError: validargs not defined
Stacktrace:
 [1] Re_Date(::Int64, ::Int64, ::Int64) at .\REPL[3]:2
 [2] top-level scope at none:0
julia> Re_Date(2000, 12)
ERROR: UndefVarError: validargs not defined
Stacktrace:
 [1] Re_Date(::Int64, ::Int64, ::Int64) at .\REPL[3]:2 (repeats 2 times)
 [2] top-level scope at none:0

julia>
julia> versioninfo()
Julia Version 0.7.0
Commit a4cb80f3ed (2018-08-08 06:46 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, haswell)

It looks like that section of the manual is a bit out-of-date. You’ll need:

using Dates

and you’ll need to replace validargs with Dates.validargs.

1 Like

It looks like those functions, like validargs, are not exported from the Dates module. This works:

julia> function Date(y::Int64, m::Int64=1, d::Int64=1)
           err = Dates.validargs(Date, y, m, d)
           err === nothing || throw(err)
           return Date(Dates.UTD(Dates.totaldays(y, m, d)))
       end
Date

julia> Date(2000)
2000-01-01

I think you weren’t intended to actually run the function Date code. It’s just showing you how the method was defined in the Dates module. The code works fine if you don’t run it.

3 Likes

Here is the current status of: “Dates.validargs()”

using Dates
function Date(y::Int64, m::Int64=1, d::Int64=1)
    err = Dates.validargs(Date, y, m, d)
    err === nothing || throw(err)
    return Date(UTD(totaldays(y, m, d)))
end
Date(2000, 12, 12)

Throws:

ERROR: MethodError: no method matching validargs(::typeof(Date), ::Int64, ::Int64, ::Int64)
[...]
versioninfo()
Julia Version 1.7.2

Any ideas?