https://github.com/jonniedie/concretestructs.jl
🏩🏠🌆🏨🌇🏦
https://github.com/jonniedie/concretestructs.jl
Last synced: 3 months ago
JSON representation
🏩🏠🌆🏨🌇🏦
- Host: GitHub
- URL: https://github.com/jonniedie/concretestructs.jl
- Owner: jonniedie
- License: mit
- Created: 2020-09-25T03:53:08.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-11T17:10:21.000Z (almost 2 years ago)
- Last Synced: 2025-02-23T05:48:56.808Z (4 months ago)
- Language: Julia
- Size: 202 KB
- Stars: 82
- Watchers: 5
- Forks: 2
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ConcreteStructs.jl
| **Documentation** | **Build Status** |
|:-------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------:|
| [![][docs-stable-img]][docs-stable-url] [![][docs-dev-img]][docs-dev-url] | [![][github-img]][github-url] [![][codecov-img]][codecov-url] |[docs-dev-img]: https://img.shields.io/badge/docs-latest-blue.svg
[docs-dev-url]: https://jonniedie.github.io/ConcreteStructs.jl/dev[docs-stable-img]: https://img.shields.io/badge/docs-stable-blue.svg
[docs-stable-url]: https://jonniedie.github.io/ConcreteStructs.jl/stable[github-img]: https://img.shields.io/github/workflow/status/jonniedie/ConcreteStructs.jl/CI
[github-url]: https://github.com/jonniedie/ConcreteStructs.jl/actions/workflows/CI[codecov-img]: https://img.shields.io/codecov/c/github/jonniedie/ConcreteStructs.jl
[codecov-url]: https://codecov.io/gh/jonniedie/ConcreteStructs.jlConcreteStructs.jl exports the macro `@concrete`, which can be used to make non-concrete structs
concrete without the boilerplate of adding type parameters.```julia
using ConcreteStructs@concrete struct AB
a
b
end
```
```julia-repl
julia> ab = AB("hi", 1+im)
AB{String,Complex{Int64}}("hi", 1 + 1im)
```
The macro also supports the `terse` keyword to allow the types to show without their parameters while in `:compact => true` mode of an `IOContext`.
```julia
@concrete terse mutable struct CDE{D} <: Number
d::D
c
e::Symbol
end
```
```julia-repl
julia> cde = CDE(1f0, (1,2.0), :yo)
CDE(1.0f0, (1, 2.0), :yo)julia> typeof(cde)
CDE{Float32,Tuple{Int64,Float64}}
```
Types with more complicated parameterizations are also handled
```julia
@concrete terse struct FGH{T,N,G<:AbstractArray{T,N}} <: AbstractArray{T,N}
g::G
h::T
f
function FGH(g::AbstractArray{T,N}, h::H, f) where {T,N,H}
Tnew = promote_type(T, H)
g, h = Tnew.(g), Tnew(h)
return new{Tnew,N,typeof(g),typeof(f)}(g, h, f)
end
endBase.size(x::FGH) = size(x.g)
Base.getindex(x::FGH, i...) = getindex(x.g[i...])
```
```julia-repl
julia> fgh = FGH([1,2,3], 4, nothing)
3-element FGH:
1
2
3julia> typeof(fgh)
FGH{Int64,1,Array{Int64,1},Nothing}
```## Attributions