Skip to content

Build types in Julia where some fields have computed types

License

Notifications You must be signed in to change notification settings

vtjnash/ComputedFieldTypes.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Build types in Julia where some fields have computed types

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages