# Constraints on parametrized abstract types

**URL:** <https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 7, 2017, 3:26pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344 "2017-01-07T15:26:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 7, 2017, 3:26pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/1 "2017-01-07T15:26:56Z")

</div>

Is there any mechanism for constraining numerical parameters of abstract types?

```julia
abstract Var{dim<:Integer} # can we enforce dim > 0 ?

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [January 7, 2017, 4:59pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/2 "2017-01-07T16:59:04Z")

</div>

I’m not aware of such a capacity, but `dim <: Integer` doesn’t make any sense since `<:` compares two types, but dim isn’t a type.

I wouldn’t worry about this. It’s not going to be the main source of bugs in any program you write.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 7, 2017, 5:04pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/3 "2017-01-07T17:04:41Z")

</div>

@johnmyleswhite isn’t `dim<:Integer` the syntax to constrain the parameter type in a declaration? What is the effect of writing it?

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [January 7, 2017, 5:11pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/4 "2017-01-07T17:11:27Z")

</div>

In type definitions, that syntax checks that `dim` is a specific type (like `Int64`) and doesn’t check that `dim` is a specific number.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 7, 2017, 5:15pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/5 "2017-01-07T17:15:52Z")

</div>

I know, that is the reason I asked the question. I know how to constrain types, but not values.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 7, 2017, 6:31pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/6 "2017-01-07T18:31:07Z")

</div>

In principle you could define a new type `PositiveInteger` as

```julia
immutable PositiveInteger <: Integer
     i::Int

     PositiveInteger(i) = i > 0 ? new(i) : throw(ArgumentError("Only positive integers allowed"))
end

```

and restrict the type to be a subtype of `PositiveInteger`.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 7, 2017, 6:38pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/7 "2017-01-07T18:38:12Z")

</div>

Thank you @dpsanders I was just wondering if such behavior could be enforced with syntax. I agree with @johnmyleswhite when he says that this enforcement is not something I should be worried about. It is very unlikely that a user will misuse the type with a negative dimension.

---

<div class="post-metadata">

**Author:** ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)\
**Post date:** [January 7, 2017, 7:10pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/8 "2017-01-07T19:10:53Z")

</div>

I’ve been thinking about the same thing for the [Tau.jl](https://github.com/Aerlinger/Tau.jl) package. The idea there is to overload operators like `*` to yield `pi*2 == tau`, but allowing the remaining values to work as usual, i.e. converting to float. This is based on a comment by @StefanKarpinski [here](https://github.com/toivoh/PatternDispatch.jl):

> Perhaps that’s good reason to have tau in Base: it’s a more exact way to express 2pi as an Irrational.

So far it seems I’ll have to rely on [PatternDispatch.jl](https://github.com/toivoh/PatternDispatch.jl), but I’ll be following this thread closely in case something else comes up 🙂

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 7, 2017, 10:17pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/9 "2017-01-07T22:17:43Z")

</div>

> [@juliohm](#):
>
> I know, that is the reason I asked the question. I know how to constrain types, but not values.

The confusion stems from a slight sloppiness in you question. You were referring to `dim` both as a type (parameter) _and_ as a value:

> [@juliohm](#):
>
> abstract Var{dim\<:Integer} # can we enforce dim \> 0 ?

I think you can dispatch on values using “value types”: [http://docs.julialang.org/en/stable/manual/types/#value-types](http://docs.julialang.org/en/stable/manual/types/#value-types) , but I don’t think that works for “greater than”, only equality. Also, I hear it’s not a good idea, in most cases.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 8, 2017, 5:09pm UTC](https://discourse.julialang.org/t/constraints-on-parametrized-abstract-types/1344/10 "2017-01-08T17:09:05Z")

</div>

Thank you @DNF, I thought of value types too initially, but as you said they only work with equalities.
