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)
Please post questions to the Julia discourse discussion forum.
I think the manual is actually out of date in this case. Dates doesn't export validargs, so it should be Dates.validargs instead of just validargs.
This just shows the implementation of a function in the Dates module, it is not something you should define yourself. But if it is confusing, perhaps a toy example would be better.
This is not about the Dates module, it is about the default parameters and how they fail when we pass optional arguments
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
The problem is that validargs is not defined in Main (where you run the code). It is defined in Dates. It has nothing to do with optional arguments.
Thanks so much @KristofferC.
So would this function be a clean function to test with
function foo(y::Int64, m::Int64=1, d::Int64=5)
a = y+m+d
printf(a)
return a
end
Yeah, except that we don't have printf :)
I have tried to run it but it fails.
we don't have
printf:)
Sorry about that. Revised
function foo(y::Int64, m::Int64=1, d::Int64=5)
a = y+m+d
return a
end
I have tried this on mac and it pass but still fails on windows
versioninfo()
Julia Version 0.7.0
Commit a4cb80f3ed (2018-08-08 06:46 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.0 (ORCJIT, broadwell)
Environment:
JULIA_PATH = /usr/local/julia
JULIA_GPG = 3673DF529D9049477F76B37566E3C7DC03D6E495
JULIA_VERSION = 0.7.0
But fails on windows
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)
Please post questions to the Julia discourse discussion forum.
Indeed, please, continue the conversation on discourse.
Most helpful comment