-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added docs. Made constructors work better.
- Loading branch information
Showing
11 changed files
with
319 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "ConcreteStructs" | ||
uuid = "2569d6c7-a4a2-43d3-a901-331e8e4be471" | ||
authors = ["Jonnie Diegelman <[email protected]> and contributors"] | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
|
||
[compat] | ||
julia = "1.1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build/ | ||
site/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[deps] | ||
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Documenter | ||
using ConcreteStructs | ||
|
||
makedocs( | ||
modules = [ConcreteStructs], | ||
sitename = "ConcreteStructs.jl", | ||
pages =[ | ||
"Home" => "index.md", | ||
"Walkthrough" => "walkthrough.md", | ||
"API" => "api.md", | ||
], | ||
format = Documenter.HTML( | ||
canonical = "https://jonniedie.github.io/ConcreteStructs.jl/stable", | ||
), | ||
repo="https://github.com/jonniedie/ConcreteStructs.jl/blob/{commit}{path}#L{line}", | ||
authors = "Jonnie Diegelman", | ||
assets = String[], | ||
) | ||
|
||
# Documenter can also automatically deploy documentation to gh-pages. | ||
# See "Hosting Documentation" and deploydocs() in the Documenter manual | ||
# for more information. | ||
deploydocs( | ||
repo = "github.com/jonniedie/ConcreteStructs.jl.git" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Documenter | ||
using ConcreteStructs | ||
|
||
makedocs( | ||
modules = [ConcreteStructs], | ||
sitename = "ConcreteStructs.jl", | ||
pages =[ | ||
"Home" => "index.md", | ||
"Walkthrough" => "walkthrough.md", | ||
"API" => "api.md", | ||
], | ||
format = Documenter.HTML( | ||
canonical = "https://jonniedie.github.io/ConcreteStructs.jl/stable", | ||
prettyurls=false, | ||
), | ||
repo="https://github.com/jonniedie/ConcreteStructs.jl/blob/{commit}{path}#L{line}", | ||
authors = "Jonnie Diegelman", | ||
assets = String[], | ||
) | ||
|
||
# Documenter can also automatically deploy documentation to gh-pages. | ||
# See "Hosting Documentation" and deploydocs() in the Documenter manual | ||
# for more information. | ||
deploydocs( | ||
repo = "github.com/jonniedie/ConcreteStructs.jl.git" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```@docs | ||
@concrete | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# ConcreteStructs.jl | ||
|
||
ConcreteStructs exports the macro `@concrete` that will add type parameters to your struct for any field where type parameters arenβt given. | ||
|
||
Simply add the `@concrete` macro before any valid `struct` definition and it should automagically make all of your non-type-annotated fields type-annotated. If you don't like the verbose type printing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Walkthrough | ||
The `@concrete` macro can be tacked onto any `struct` definition that isn't already concretely-typed. That's kinda all there is to it, but let's walk through some examples anyway to get a feel for it. | ||
|
||
In this example, no type parameters are given, so they will be filled in automatically: | ||
|
||
```julia | ||
@concrete struct Whatever | ||
a | ||
b | ||
end | ||
``` | ||
```julia-repl | ||
julia> complex_whatever = Whatever(1+im, "It's pretty complex") | ||
Whatever{Complex{Int64},String}(1 + 1im, "It's pretty complex") | ||
``` | ||
|
||
But maybe we don't want to show the type parameters, since we never cared much about them in the first place. If we want our `struct` to print a little more succintly, we can add the `terse` keyword. Now it will print as if we never added the `@concrete` macro. | ||
|
||
```julia | ||
@concrete terse struct PrettierWhatever | ||
a | ||
b | ||
end | ||
``` | ||
```julia-repl | ||
julia> pretty_whatever = PrettierWhatever(1+im, "It's still pretty complex") | ||
PrettierWhatever(1 + 1im, "It's still pretty complex") | ||
``` | ||
The full type information is still available for inspection with the `typeof` function, though. | ||
```julia-repl | ||
julia> typeof(pretty_whatever) | ||
PrettierWhatever{Complex{Int64},String} | ||
``` | ||
|
||
More complicated type parameterizations are possible as well. Take this example of an array with two metadata fields attached. The type parameters for the `array` field are provided but the `name` field's type is left open. The `@concrete` macro will respect the given type parameters and concretely parameterize the `name` field. | ||
|
||
```julia | ||
@concrete struct MetaArray{T,N,A<:AbstractArray{T,N}} <: AbstractArray{T,N} | ||
array::A | ||
name | ||
end | ||
|
||
Base.size(x::MetaArray) = size(x.array) | ||
|
||
Base.getindex(x::MetaArray, i...) = getindex(x.array[i...]) | ||
``` | ||
```julia-repl | ||
julia> abed = MetaArray([8,10,2,5], "Abed") | ||
4-element MetaArray{Int64,1,Array{Int64,1},String}: | ||
8 | ||
10 | ||
2 | ||
5 | ||
``` | ||
|
||
We can also have type parameters that don't correspond to any field. In this example, the `BananaStand` type is parameterized by the boolean value `has_money`. | ||
|
||
```julia | ||
@concrete terse mutable struct BananaStand{has_money} | ||
employees | ||
manager | ||
end | ||
``` | ||
|
||
In this case, the constructor must be given with the `has_money` parameter, just like it would need to be if we weren't using the `@concrete` macro. Since the `terse` keyword was give, the type will print exactly as it's specified: with the `has_money` parameterization but no field parameterizations. | ||
|
||
```julia-repl | ||
julia> the_banana_stand = BananaStand{true}(["Maeby", "Annyong"], "George Michael") | ||
BananaStand{true}(["Maeby", "Annyong"], "George Michael") | ||
julia> typeof(the_banana_stand) | ||
BananaStand{true,Array{String,1},String} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
d060a20
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
d060a20
There was a problem hiding this comment.
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/22344
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 the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: