Skip to content

Commit

Permalink
Add tuples as hash
Browse files Browse the repository at this point in the history
  • Loading branch information
josePereiro committed May 12, 2024
1 parent 7f64013 commit 768ad10
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,17 @@ file = dfname(["dir1", "dir2"], "my_cool_file", (;A = 1), ".jls")
@show file = # file = "dir1/dir2/my_cool_file [A=1].jls"
```

'Simple' tuples are hashed. This is useful for quick unique data based file names.

```julia
file = dfname("comment", (1,2,3), ".ext")
@show file = # file = "comment [hash=0x83d91fbc7a900a8f].ext"
```

To avoid such behavior (if you wanna use a vector as part of the name) use an empty string.

```julia
# Empty strings are ignored in the construction of the name but are relevant is the arguments structure. Now the vector is taken as an ordinary argument and `parse_arg(v::Vector{String})` must be implemented
# Empty strings are ignored in the construction of the name but are relevant in the arguments structure. Now the vector is taken as an ordinary argument and `parse_arg(v::Vector{String})` must be implemented
file = dfname("", ["dir1", "dir2"], "my_cool_file", (;A = 1), ".jls")
# ERROR: LoadError: parse_arg(v::Vector{String}) not implemented. Type `?parse_arg` for help.
```
Expand Down
8 changes: 8 additions & 0 deletions src/dfname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ parse_arg(pt::NamedTuple) = pt
parse_arg(pt::Iterators.Pairs) = parse_arg(Dict(pt...))
parse_arg(pt::Dict) = pt
parse_arg(v::Any) = error("parse_arg(v::", typeof(v), ") not implemented. Type `?parse_arg` for help.")
# A tupple will be hashed
function parse_arg(pt::Tuple)
h = hash(0)
for el in pt
h = hash(el, h)
end
return (;hash = repr(h))
end

# -------------------------------------------------------------------
# _argstr
Expand Down

0 comments on commit 768ad10

Please sign in to comment.