# Type constraints in Distributions and StatsFuns

**URL:** <https://discourse.julialang.org/t/type-constraints-in-distributions-and-statsfuns/22663>\
**Category:** Tooling\
**Created:** [April 3, 2019, 12:02am UTC](https://discourse.julialang.org/t/type-constraints-in-distributions-and-statsfuns/22663 "2019-04-03T00:02:50Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [April 8, 2019, 4:37pm UTC](https://discourse.julialang.org/t/type-constraints-in-distributions-and-statsfuns/22663/12 "2019-04-08T16:37:09Z")

</div>

Alright, so I went ahead and [implemented](https://github.com/chakravala/Reduce.jl/commit/33f234cbc9560142b57f7bf95a8041dd0f91152f) the requested feature, which works on `master` branch

```nohighlight
julia> Reduce.@subtype FakeReal <: Real

julia> FakeReal(R"x+1") + FakeReal(R"y")
y + 1 + x

```

the implementation looks like this at the moment

```nohighlight
macro subtype(x)
    x.head ≠ :<: && throw(error("$x is not a subtype expression"))
    name = x.args[1]
    Expr(:struct,false,x,Expr(:block,Expr(:(::), :r, :RExpr))) |> eval
    @eval begin
        export $name
        show(io::IO, r::$name) = show(io,r.r)
        extract(r::$name) = r.r
        Algebra.init_subtype($name)
    end
    nothing
end

```

and

```nohighlight
function init_subtype(name)
    Expr(:block,[:(Base.$i(r::$name...)=$i(extract.(r)...)|>$name) for i ∈ [alg;iops]]...) |> eval
    Expr(:block,[:($i(r::$name...)=$i(extract.(r)...)|>$name) for i ∈ [calculus;cnan;cmat]]...) |> eval
    Expr(:block,[:(Base.$i(r::$name)=$i(extract(r))|>$name) for i ∈ [sbas;[:length]]]...) |> eval
    Expr(:block,[:($i(r::$name)=$i(extract(r))|>$name) for i ∈ [sfun;snan;snum;scom;sint;sran;smat]]...) |> eval
end

```

this provides all of the basic functions from the `Reduce.Algebra` module, but outside that, the new subtypes generated do not have the same treatment ast `RExpr` at the moment

---

_[View the full topic](https://discourse.julialang.org/t/type-constraints-in-distributions-and-statsfuns/22663)._
