Custom Subtyping in Julia
Author bhgomes
3 Stars
Updated Last
2 Years Ago
Started In
October 2019


Stable Docs Latest Docs Travis Build Status Appveyor Build status Formatting


Custom Subtyping in Julia


To install, run the following in a Julia session:

]add SubTypes

Custom Subtype

To create a custom subtype define the following data

SubType{T, P, Ctx}
        ^  ^  ^
        |  |  |
        |  |  L ______ Type Context
        |  L _________ Predicate Data
        L ____________ Underlying Type

and a predicate processing function

check_predicate(P, Val(Ctx), x::T)

which checks if x::T should be of type SubType{T, P, Ctx}. The context Ctx is the way to mark the subtypes for overloading.

Predefined Custom Subtypes

The custom subtypes Constrained and ConstrainedSymbol are predefined in the SubType module. They emulate set inclusion subtyping, i.e.

x::Constrained{T, S}  <=>  x.value::T in S

The ConstrainedSymbol type emulates set inclusion for Symbol types. The Constrained type is defined as follows:

const Constrained{T,S} = SubType{T,S,:Constrained}

Inspiration for this type comes from this post by Mohamed Tarek @mohamed82008.

Helper Functions

This module also comes with these helper functions defined for terms and types:

Helper Function Component Description
eltype SubType{T} => T The underlying type where the subtype terms are drawn from.
predicate SubType{T,P} => P The predicate data which determines the subtype terms.
context SubType{T,P,Ctx} => Ctx The implementation label for the check_predicate function.
support Constrained{T,S} => S The underlying set where the constrained variables are constrained to.

Author License GitHub Repo