defaultkeywordarguments.jl
A package to handle default keyword arguments in Julia
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (8.7%) to scientific vocabulary
Repository
A package to handle default keyword arguments in Julia
Basic Info
- Host: GitHub
- Owner: PdIPS
- License: mit
- Language: Julia
- Default Branch: main
- Homepage: https://pdips.github.io/DefaultKeywordArguments.jl/
- Size: 405 KB
Statistics
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 2
Metadata Files
README.md
DefaultKeywordArguments.jl
DefaultKeywordArguments.jl is a package to handle default keyword arguments in Julia. It has been developed to support CBX.jl.
The @default Macro
In Julia, functions can have keyword arguments with a default value. The function
jl
with_keywords(x; a = 2.0, b = 3.0, c = 4.0) = (a * x + b) / c;
will return (2.0 * x + 3.0) / 4.0 unless you specify a different value of a, b, or c.
When you're writing a complex piece of software, you may have multiple functions that use the keyword arguments a, b, and c, and which should all use the same default value. For instance:
jl
with_keywords(x; a = 2.0, b = 3.0, c = 4.0) = (a * x + b) / c;
another_with_keywords(x; a = 2.0, b = 3.0, c = 4.0) = round(Int, (a * x + b) / c);
yet_another_with_keywords(x; a = 2.0, b = 3.0, c = 4.0) = round(Int, c / (a * x + b));
If you have many functions and many keyword arguments, maintaining consistency of the default values can soon become cumbersome. One sensible option would be to collect all the default values in a single place:
jl
const default_values = (; a = 2.0, b = 3.0, c = 4.0);
with_keywords(x; a = default_values.a, b = default_values.b, c = default_values.c) = (a * x + b) / c;
another_with_keywords(x; a = default_values.a, b = default_values.b, c = default_values.c) = round(Int, (a * x + b) / c);
yet_another_with_keywords(x; a = default_values.a, b = default_values.b, c = default_values.c) = round(Int, c / (a * x + b));
However, writing a = default_values.a, b = default_values.b, and c = default_values.c over and over is tedious, and can clutter your code.
The @default macro offers an alternative:
jl
const default_values = (; a = 2.0, b = 3.0, c = 4.0);
@default default_values with_keywords(x; a, b, c) = (a * x + b) / c;
@default default_values another_with_keywords(x; a, b, c) = round(Int, (a * x + b) / c);
@default default_values yet_another_with_keywords(x; a, b, c) = round(Int, c / (a * x + b));
You can write non-compact functions instead:
jl
const default_values = (; a = 2.0, b = 3.0, c = 4.0);
@default default_values function with_keywords(x::Float64; a, b, c)
return (a * x + b) / c
end
You can also write type annotations and default values as usual. Furthermore, you can overwrite the default values of each keyword argument individually, if required. These are all valid:
```jl
const default_values = (; a = 2.0, b = 3.0, c = 4.0);
x must be a Float64
@default defaultvalues withkeywords(x::Float64; a, b, c) = (a * x + b) / c;
x has a default value of 7
@default defaultvalues anotherwith_keywords(x = 7; a, b, c) = round(Int, (a * x + b) / c);
the default value of a is overriden to 17.5
@default defaultvalues yetanotherwithkeywords(x; a = 17.5, b, c) = round(Int, c / (a * x + b)); ```
The @config Macro
You might require a more advanced version of parameter handling, where you just pass a config object which propagates across your functions, and then they selectively use default values for certain variables whenever they are not available in config:
```jl
function firstcall(config)
a = (haskey(config, :a)) ? config.a : 2.0
b = (haskey(config, :b)) ? config.b : 3.0
# some code
return secondcall(config)
end
function secondcall(config)
a = (haskey(config, :a)) ? config.a : 2.0
c = (haskey(config, :c)) ? config.c : 4.0
# some more code
end
``
Callingmyconfig = (; a = 1.0); firstcall(myconfig)would use your custom value ofain both functions, but use the default values ofbandc` when required.
A way to maintain consistency of the default values would be to replace this with: ```jl const default_config = (; a = 2.0, b = 3.0, c = 4.0);
function firstcall(config) return firstcall_expanded(config; config...) end
function firstcallexpanded(config; a = defaultconfig.a, b = defaultconfig.b, args...) b = (haskey(config, :b)) ? config.b : 3.0 # some code return second_call(config) end
function secondcall(config) return secondcall_expanded(config; config...) end
function secondcallexpanded(config; a = defaultconfig.a, c = defaultconfig.c, args...)
# some more code
end
This code is verbose and repetitive. However, it can be generated by the `@config` macro instead:
jl
const default_config = (; a = 2.0, b = 3.0, c = 4.0);
@config defaultconfig function firstcall(; a, b) # some code return second_call(config) end
@config defaultconfig function secondcall(; a, c)
# some more code
end
``
To avoid repetition, **you don't even have to specify theconfigargument** on each function. Callingmyconfig = (; a = 1.0); firstcall(my_config)` will behave as in the previous code.
Once again, you are allowed to have extra arguments, type annotations, or default values, as you would in any other Julia function.
:warning: The code pattern generated by @config allocates some heap memory. This macro should be used for high-level functions that are not performance-critical.
Owner
- Name: Purpose-driven particle systems
- Login: PdIPS
- Kind: organization
- Repositories: 1
- Profile: https://github.com/PdIPS
Citation (citation.cff)
cff-version: 1.2.0
title: 'DefaultKeywordArguments.jl'
message: 'If you use this software, please cite it as below.'
authors:
- family-names: 'Bailo'
given-names: 'Rafael'
orcid: 'https://orcid.org/0000-0001-8018-3799'
type: software
version: 1.1.0
url: 'https://github.com/PdIPS/DefaultKeywordArguments.jl'
GitHub Events
Total
- Delete event: 5
- Push event: 12
- Pull request event: 11
- Create event: 6
Last Year
- Delete event: 5
- Push event: 12
- Pull request event: 11
- Create event: 6
Packages
- Total packages: 1
-
Total downloads:
- julia 3 total
- Total dependent packages: 1
- Total dependent repositories: 0
- Total versions: 2
juliahub.com: DefaultKeywordArguments
A package to handle default keyword arguments in Julia
- Homepage: https://pdips.github.io/DefaultKeywordArguments.jl/
- Documentation: https://docs.juliahub.com/General/DefaultKeywordArguments/stable/
- License: MIT
-
Latest release: 1.1.1
published over 2 years ago
Rankings
Dependencies
- actions/checkout v3 composite
- codecov/codecov-action v3 composite
- julia-actions/cache v1 composite
- julia-actions/julia-buildpkg v1 composite
- julia-actions/julia-docdeploy v1 composite
- julia-actions/julia-processcoverage v1 composite
- julia-actions/julia-runtest v1 composite
- julia-actions/setup-julia v1 composite
- JuliaRegistries/TagBot v1 composite