# How to make a particular NTuple based Type concrete

**URL:** <https://discourse.julialang.org/t/how-to-make-a-particular-ntuple-based-type-concrete/50846>\
**Category:** General Usage\
**Tags:** question, ntuple\
**Created:** [November 27, 2020, 4:19am UTC](https://discourse.julialang.org/t/how-to-make-a-particular-ntuple-based-type-concrete/50846 "2020-11-27T04:19:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [November 27, 2020, 4:19am UTC](https://discourse.julialang.org/t/how-to-make-a-particular-ntuple-based-type-concrete/50846/1 "2020-11-27T04:19:44Z")

</div>

I have a struct representing the remainders of an integer with respect to several moduli. All of the moduli fit into `Int64` except for `2^64`, so every modulus is either an `Int64` or `Type{Int64}`.

```julia
struct LooseGoose{N} 
    remainders::NTuple{N,Int64}
    moduli::NTuple{N,Union{DataType,Int64}}
end
import Base: *
*(g::LooseGoose, r::Int64) = LooseGoose( mod.( widen.(g.remainders) .* r , g.moduli) .|> Int64 , g.moduli )
LooseGoose((4,7,12),(Int64,11,17)) * 7

```

This works fine, except in practice I always want the first modulus to be `Int64` and the remaining moduli to be numeric. Is there a convenient way to specify this in the type definition without the `Union`. I can enforce this in a constructor.

```julia
struct StrictGoose{N} 
    remainders::NTuple{N,Int64}
    moduli::NTuple{N,Union{DataType,Int64}}    

    function StrictGoose(remainders::NTuple{N,Int64}, moduli) where {N}
        moduli isa Tuple{DataType, (Int64 for i=2:N)...} || throw(ArguementError("moduli of incorrect form"))
        new{N}(remainders,moduli)
    end 
end
StrictGoose((4,7,12),(Int64,11,17)) # Succeeds
StrictGoose((4,7,12),(13,Int64,17)) # Throws an error

```

1. Is there a way to eliminate the `Union` and specify `Tuple{DataType, (Int64 for i=2:N)...}` more directly in the type definition?
2. If the type of `moduli` is going to be restricted by the constructor anyway should I eliminate the annotation in the type declaration?
3. Would I be better off using `0` as a sentinel value for `Int64` so my `Goose` can be `isbitstype`, but then having to replace `mod` with something that watches for this value?

```julia
modif(a,b) = b==0 ? a : mod(a,b)

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 27, 2020, 4:44am UTC](https://discourse.julialang.org/t/how-to-make-a-particular-ntuple-based-type-concrete/50846/2 "2020-11-27T04:44:17Z")

</div>

Do not be concerned that using  
`modnz(a,b) = iszero(b) ? a : mod(a,b)` instead of `mod(a,b)`  
would impact performance.  
Do prefer working with an `isbitstype(type)` where performance matters and there is much computation is done.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [November 27, 2020, 6:21am UTC](https://discourse.julialang.org/t/how-to-make-a-particular-ntuple-based-type-concrete/50846/3 "2020-11-27T06:21:17Z")

</div>

> [@malacroi](#):
>
> I always want the first modulus to be `Int64` and the remaining moduli to be numeric. Is there a convenient way to specify this in the type definition without the `Union` .

You could do:

```julia
struct Goose{M} 
    remainders::Tuple{Int64,Vararg{Int64,M}}
    moduli::Tuple{Type{Int64},Vararg{Int64,M}}
end

```

but note that `M` is one less than your `N`.

---

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [November 27, 2020, 6:27am UTC](https://discourse.julialang.org/t/how-to-make-a-particular-ntuple-based-type-concrete/50846/4 "2020-11-27T06:27:47Z")

</div>

`M` being smaller than `N` is probably a feature here, since this formulation conveniently forces me to rethink whether I ever need 0-Tuples, which, of course, I don’t.
