TypeVars.jl

Type Variables are objects associated with parameterized types
Author KlausC
Popularity
0 Stars
Updated Last
4 Years Ago
Started In
February 2021

TypeVars

Build Status Coverage Status

Introduction

Sometimes it is useful to associate a data object with a parameterized type. For example, the type Zmod{m} would represent integer calculations modulo m where m is a samll integer.

This tiny package provides a way to extend this if type parameters are not feasible to keep the data.

API

There are 2 functions that make the user interface:

settypevar(::Type, object) is called once per type variant to define the association.

typevar(::Type) makes the object available in an efficient way.

Usage Example

using TypeVars

struct Zmod{M,T<:Integer>}
    val::T
end

m = big"2"^512 - 1
M = Symbol(hash(m))
settypevar(Zmod{M,BigInt}, m)

add(a::Z, b::Z) where Z<:Zmod = Zmod(mod(a.val + b.val, typevar(Z)))

Implementation

The obvious idea of using a global Dict suffers from access time overhead. We make typevar a generated function to levearge this access.