# Argument checking in user defined types

**URL:** https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950
**Category:** General Usage
**Tags:** documentation
**Created:** [June 25, 2018, 10:01pm UTC](https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950 "2018-06-25T22:01:05Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)
#### Post date: [June 25, 2018, 10:01pm UTC](https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950/1 "2018-06-25T22:01:05Z")

</div>

Hi.  
Sorry if this is obvious or belies a complete misunderstanding of the Julia way… but I was trying to figure out how to do argument checking in a user defined type. For example, to enforce that a value be a positive valued number.

As a relative beginner (a Matlab convert) I didn’t really know how to do this. The closest I got in searching the documentation was that it should be done as a constructor, so I need some kind of function in my user defined type.

I managed to find an example from Distributions.jl where you define a normal distribution, where sigma has to be positive

```julia
struct Normal{T<:Real} <: ContinuousUnivariateDistribution
    μ::T
    σ::T

    Normal{T}(μ, σ) where {T} = (@check_args(Normal, σ > zero(σ)); new{T}(μ, σ))
end

```

So this is pretty opaque to me as a beginner, and it seems like `@check_args` is a macro specific to the Distributions.jl package?

I also found that there is the [ArgCheck.jl](https://github.com/jw3126/ArgCheck.jl) package which looks promising. But my hunch is that there should be a way of doing this with base Julia.

Anyway, I guess my friendly point here is that as a beginner approaching this pretty routine task I didn’t really get very far. If anyone could point me towards an example, or provide an answer, that would be cool. My hunch is that quite a lot of newcomers to Julia would probably appreciate a little worked example in the docs.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 25, 2018, 10:13pm UTC](https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950/2 "2018-06-25T22:13:11Z")

</div>

There’s an example in the manual which constructs an ordered pair, subject to the constraint that the pair is, in fact, ordered: [Constructors · The Julia Language](https://docs.julialang.org/en/v0.6.2/manual/constructors/#Inner-Constructor-Methods-1)

Does that help?

---

<div class="post-metadata">

### Author: ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)
#### Post date: [June 25, 2018, 10:23pm UTC](https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950/3 "2018-06-25T22:23:30Z")

</div>

Ah ha! Thanks.

So as a beginner I could not mentally parse the `a ? b : c` control flow, so it just looked very odd.

But yes, now I figured that out, it looks pretty easy. So you can do something like this

```julia
struct MyThing
    whatever::Number
    positive_thing::Number
    
    MyThing(whatever, positive_thing) = positive_thing > 0 ? new(whatever, positive_thing) : error("positive_thing should be positive")
end

```

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [June 25, 2018, 10:26pm UTC](https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950/4 "2018-06-25T22:26:19Z")

</div>

That seems fine, but be careful because

```julia
struct MyThing
    whatever::Number
    positive_thing::Number

```

is not strictly typed. A better approach is either

```julia
struct MyThing{A,B}
    whatever::A
    positive_thing::B

```

or if you know the types of the things, you can do it directly

```julia
struct MyThing
    whatever::Float64
    positive_thing::Float64

```

(although first option is more general and scalable to different types)

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 25, 2018, 10:32pm UTC](https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950/5 "2018-06-25T22:32:02Z")

</div>

Yes indeed.

The ternary `a ? b : c` syntax is kind of weird if you’re not used to it from a language like C. I personally find the more verbose version clearer in this case:

```julia
struct MyThing
  positive_thing::Number
  function MyThing(positive_thing)
    if positive_thing > 0
      return new(positive_thing)
    else
      error("Should be positive")
    end
  end
end

```

although as @Datseris says, using `MyThing{T <: Number}; positive_thing::T` will generally perform better. See: [https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-fields-with-abstract-type-1](https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-fields-with-abstract-type-1)

---

<div class="post-metadata">

### Author: ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)
#### Post date: [June 25, 2018, 10:42pm UTC](https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950/6 "2018-06-25T22:42:03Z")

</div>

I agree, I also find that clearer. So a few things going on there…

1. Is that validation function automatically run (not simply defined) because it is declared in a `struct`?
2. Your suggestion is to be more specific and essentially create a new `PositiveNumber` type so you are dealing with one field at a time, then you can enforce that some of your types in other larger composition types are `::PositiveNumber` ? Or was that just a simplification in your example?

For the sake of learning, I checked and it looks like I can do it in one…

```julia
struct MyThing
  whatever::Float64
  positive_thing::Float64
  function MyThing(whatever, positive_thing)
    if positive_thing > 0
      return new(whatever, positive_thing)
    else
      error("positive_thing should be positive")
    end
  end
end

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 25, 2018, 10:56pm UTC](https://discourse.julialang.org/t/argument-checking-in-user-defined-types/11950/7 "2018-06-25T22:56:16Z")

</div>

1. That function is an “inner constructor” (see [Constructors · The Julia Language](https://docs.julialang.org/en/v0.6.2/manual/constructors/#Inner-Constructor-Methods-1) ), so it’s the lowest-level way to construct a `MyThing`. I’m just using the syntax:

```julia
function foo(x, y)
  x + y
end

```

instead of the shorter but exactly equivalent syntax:

```julia
foo(x, y) = x + y

```

which is used in that particular example in the manual.

1. I was just simplifying. But having a `PositiveNumber{T <: Number}` type that does all your positivity-checking would also be a reasonable thing to do.
