https://github.com/bhgomes/subtypes.jl
Custom Subtyping in Julia
https://github.com/bhgomes/subtypes.jl
custom-types julia julia-language subtype subtypes typing
Last synced: 4 months ago
JSON representation
Custom Subtyping in Julia
- Host: GitHub
- URL: https://github.com/bhgomes/subtypes.jl
- Owner: bhgomes
- License: unlicense
- Created: 2019-10-29T15:38:51.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-17T18:06:12.000Z (over 4 years ago)
- Last Synced: 2025-01-29T05:17:55.452Z (6 months ago)
- Topics: custom-types, julia, julia-language, subtype, subtypes, typing
- Language: Julia
- Homepage:
- Size: 39.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# SubTypes.jl
[](https://bhgomes.github.io/SubTypes.jl/stable)
[](https://bhgomes.github.io/SubTypes.jl/latest)
[](https://travis-ci.com/bhgomes/SubTypes.jl)
[](https://ci.appveyor.com/project/bhgomes/subtypes-jl)
[](https://github.com/domluna/JuliaFormatter.jl)[](https://doi.org/10.5281/zenodo.3525301)
_Custom Subtyping in Julia_
## Installation
To install, run the following in a Julia session:
```julia
]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
```julia
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.
```julia
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:
```julia
const Constrained{T,S} = SubType{T,S,:Constrained}
```Inspiration for this type comes from [this post](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/7) by Mohamed Tarek [@mohamed82008](https://github.com/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. |---
[](https://github.com/bhgomes)
[](UNLICENSE)
[](https://github.com/bhgomes/SubTypes.jl)