Skip to content

Commit

Permalink
sync readme with doc page
Browse files Browse the repository at this point in the history
  • Loading branch information
mind6 committed Jul 29, 2023
1 parent 543ec9c commit d2a194c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ module M2
Supertypes inside containers must be matched with itself or a __broader__ type.
"
function pack(time::Int, ::Berry, bunch::Vector{Berry}) end

"
However, if you prefix the supertype with `<:`, it becomes a ranged parameter. You can match it with a ranged subtype parameter.
"
function move(::Vector{<:Berry}, location) end
end

@implement struct BlueBerry <: Berry end
Expand All @@ -128,21 +133,30 @@ module M2
println("packing things worth \$$(cost(first(bunch), 1.5) + cost(berry, 1.5))")
end

"
The subtype `BlueBerry` can be used in a container, because it's a ranged parameter. Make sure nested containers are all ranged parameters; otherwise, the interface cannot be satisfied.
"
function move(bunch::Vector{<:BlueBerry}, location)
println("moving $(length(bunch)) blueberries to $location")
end

@postinit function myinit()
println("docstring of imported `cost` function:\n", @doc cost)
pack(0, BlueBerry(1.0), [BlueBerry(2.0)])
move([BlueBerry(1.0), BlueBerry(2.0)], "the truck")
end
end
```
```
[ Info: Inherit.jl: processed M1 with 1 supertype having 1 method requirement. 0 subtypes were checked with 0 missing methods.
[ Info: Inherit.jl: processed M2 with 1 supertype having 2 method requirements. 1 subtype was checked with 0 missing methods.
[ Info: Inherit.jl: processed M2 with 1 supertype having 3 method requirements. 1 subtype was checked with 0 missing methods.
docstring of imported `cost` function:
this implementation satisfies the interface declaration for all subtypes of Fruit
docstrings of method declarations are appended at the end of method docstrings
packing things worth $4.5
moving 2 blueberries to the truck
```

We can make a few observations regarding the above example:
Expand All @@ -154,7 +168,7 @@ We can make a few observations regarding the above example:
- __Docstrings are preserved__. Docstring for method declarations are added to the end of any method docstrings.

!!! info
When implementing a method declaration, supertypes inside of containers like (e.g. `Pair`, `Vector`, `Dict`) _may not be_ substituted with a subtype, because Julia's type parameters are _invariant_.
When implementing a method declaration, supertypes inside of containers like (e.g. Pair, Vector, Dict) may not be substituted with a subtype, because Julia's type parameters are invariant. However, a ranged supertype parameter (prefixed with <:) can be substituted with a ranged subtype.

## Changing the reporting level

Expand Down

0 comments on commit d2a194c

Please sign in to comment.