Build types in Julia where some fields have computed types
Find a file
2023-10-16 11:03:01 -04:00
.github/workflows fix coveralls flag-name (#22) 2022-10-02 20:03:44 -04:00
src fix typo handling a field name (#24) 2023-10-16 11:01:46 -04:00
test handle where clauses and computed {} heads better (#21) 2022-01-11 23:59:46 -05:00
LICENSE Initial commit 2017-01-22 17:13:36 -05:00
Project.toml tag v1.0.2 2023-10-16 11:03:01 -04:00
README.md switch CI to GitHub Actions (#11) 2021-02-24 22:21:52 -05:00

ComputedFieldTypes

Build Status Coverage Status

Build types in Julia where some fields have computed types.

Examples

Note that the following is not idiomatic Julia, and is probably not the most efficient solutions. They are simply intended as demonstrations of ComputedFieldTypes.

For simple cases, a default constructor will be added, if none is specified:

@computed struct A{V <: AbstractVector}
    a::eltype(V)
end
a = A{Vector{Int}}(3.0)
a.a === Int(3)

It is also possible to declare your own constructor, with extra type variables, parameterized, etc.:

@computed struct B{N, M, T}
    a::NTuple{N + M, T}
    B(x::T) = new{N, M, T}(ntuple(i -> x, N + M))
    B{S}(x::S) = B{N, M, T}(convert(T, x))
end

@computed struct C{T <: Number}
    a::typeof(one(T) / one(T))
    C() = new(0)
    function C(x)
        return new(x)
    end
end

If you need a fully expanded type definition (for example, for use as a field of another @computed type), you can call fulltype(T) on any Type T. Note, however, that since this is not the canonical form, it does not have any constructors defined for it.