Skip to content

Commit

Permalink
Setup/fix doctests (#9)
Browse files Browse the repository at this point in the history
* Setup/fix doctests

* Use a consistent style for declaring struct

* Fix constructorof example
  • Loading branch information
tkf authored Oct 1, 2019
1 parent d186a75 commit ee593e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ makedocs(;
repo="https://github.com/JuliaObjects/ConstructionBase.jl/blob/{commit}{path}#L{line}",
sitename="ConstructionBase.jl",
authors="Takafumi Arakaki, Rafael Schouten, Jan Weidner",
assets=String[],
strict=true,
)

deploydocs(;
Expand Down
32 changes: 24 additions & 8 deletions src/ConstructionBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,33 @@ export constructorof
Return an object `ctor` that can be used to construct objects of type `T`
from their field values. Typically `ctor` will be the type `T` with all parameters removed:
```jldoctest
julia> struct T{A,B};a::A;b::B;end
julia> using ConstructionBase
julia> struct T{A,B}
a::A
b::B
end
julia> constructorof(T{Int,Int})
T
```
It is however not guaranteed, that `ctor` is a type at all:
```jldoctest
```jldoctest; setup = :(using ConstructionBase)
julia> struct S
a
b
checksum
S(a,b) = new(a,b,a+b)
end
julia> ConstructionBase.constructorof(S) = (a,b,checksum) -> (@assert a+b == checksum; S(a,b))
julia> ConstructionBase.constructorof(::Type{<:S}) =
(a, b, checksum=a+b) -> (@assert a+b == checksum; S(a,b))
julia> constructorof(S)(1,2)
S(1, 2, 3)
julia> constructorof(S)(1,2,4)
ERROR: AssertionError: a + b == checksum
```
Instead `ctor` can be any object that satisfies the following properties:
* It must be possible to reconstruct an object from its fields:
Expand All @@ -43,10 +52,11 @@ fieldvalues(ctor(args...)) == args
```
For instance given a suitable parametric type it should be possible to change
the type of its fields:
```jldoctest
julia> using ConstructionBase: constructorof
julia> struct T{A,B};a::A;b::B;end
```jldoctest; setup = :(using ConstructionBase)
julia> struct T{A,B}
a::A
b::B
end
julia> t = T(1,2)
T{Int64,Int64}(1, 2)
Expand Down Expand Up @@ -80,7 +90,11 @@ Return a copy of `obj` with attributes updates accoring to `patch`.
```jldoctest
julia> using ConstructionBase
julia> struct S;a;b;c; end
julia> struct S
a
b
c
end
julia> s = S(1,2,3)
S(1, 2, 3)
Expand All @@ -99,6 +113,8 @@ keywords:
# Examples
```jldoctest
julia> using ConstructionBase
julia> struct S;a;b;c; end
julia> o = S(10, 2, 4)
Expand Down

0 comments on commit ee593e5

Please sign in to comment.