Skip to content

Commit

Permalink
Update README.md (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
briochemc authored and mauro3 committed Nov 28, 2019
1 parent 79aa697 commit 77b494d
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ determined by the functions `UnPack.unpack` and `UnPack.pack!`.
The `UnPack.unpack` function is invoked to unpack one entity of some
`DataType` and has signature:

`unpack(dt::Any, ::Val{property}) -> value of property`
```julia
unpack(dt::Any, ::Val{property}) -> value of property
```

Note that `unpack` (and `pack!`) works with `Base.getproperty`. By
default this means that all the fields of a type are unpacked but if
Expand All @@ -64,7 +66,8 @@ default this means that all the fields of a type are unpacked but if
Three method definitions are included in the package to unpack a
composite type/module/NamedTuple, or a dictionary with Symbol or
string keys:
```

```julia
@inline unpack{f}(x, ::Val{f}) = getproperty(x, f)
@inline unpack{k}(x::Associative{Symbol}, ::Val{k}) = x[k]
@inline unpack{S<:AbstractString,k}(x::Associative{S}, ::Val{k}) = x[string(k)]
Expand All @@ -73,15 +76,35 @@ string keys:
The `UnPack.pack!` function is invoked to pack one entity into some
`DataType` and has signature:

`pack!(dt::Any, ::Val{field}, value) -> value`
```julia
pack!(dt::Any, ::Val{field}, value) -> value
```

Three definitions are included in the package to pack into a mutable composite
type or into a dictionary with Symbol or string keys:
```

```julia
@inline pack!{f}(x, ::Val{f}, val) = setproperty!(x, f, val)
@inline pack!{k}(x::Associative{Symbol}, ::Val{k}, val) = x[k]=val
@inline pack!{S<:AbstractString,k}(x::Associative{S}, ::Val{k}, val) = x[string(k)]=val
```

More methods can be added to `unpack` and `pack!` to allow for
specialized packing of datatypes.
specialized unpacking/packing of datatypes. Here is a MWE of customizing
`unpack`, so that it multiplies the values by 2:

```julia
using UnPack
struct Foo
a
b
end
p = Foo(1, 2)
@unpack a, b = p
a, b # gives (1, 2)

# Now we specialize unpack for our custom type, `Foo`
@inline UnPack.unpack(x::Foo, ::Val{f}) where {f} = 2 * getproperty(x, f)
@unpack a, b = p
a, b # now gives (2, 4)
```

2 comments on commit 77b494d

@mauro3
Copy link
Owner

@mauro3 mauro3 commented on 77b494d Dec 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/6234

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 77b494dd4a6010739b8908e452f109f048f56a7a
git push origin v0.1.0

Please sign in to comment.