# Can a BigInt be a type parameter?

**URL:** <https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997>\
**Category:** General Usage\
**Created:** [July 13, 2020, 7:26pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997 "2020-07-13T19:26:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [July 13, 2020, 7:26pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/1 "2020-07-13T19:26:51Z")

</div>

I am able to define a type `struct foo{N}` where `N` is an `Int`, but can I do this with `N` being a `BigInt`?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 13, 2020, 7:29pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/2 "2020-07-13T19:29:25Z")

</div>

Yes, it should be possible with any type.

Or did you encounter an issue with it?

---

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [July 13, 2020, 7:30pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/3 "2020-07-13T19:30:26Z")

</div>

Yes. I’m still struggling. Is there a good example on the web?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 13, 2020, 7:33pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/4 "2020-07-13T19:33:13Z")

</div>

```julia
struct MyType{T}
x:: T
end

MyType(big(42))

```

---

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [July 13, 2020, 7:35pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/5 "2020-07-13T19:35:02Z")

</div>

Here’s where I’m having a problem:

```julia
struct Mod{N}
    val
    function Mod(x::T, N::S) where {S<:Integer, T<:Integer}
        new{N}(mod(x,N))
    end
end

julia> Mod(10,20)
Mod{20}(10)

julia> Mod(10,big(20))
ERROR: TypeError: in Type, in parameter, expected Int64, got BigInt
Stacktrace:
 [1] Mod(::Int64, ::BigInt) at ./REPL[1]:8
 [2] top-level scope at REPL[14]:1

```

---

<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:** [July 13, 2020, 7:39pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/6 "2020-07-13T19:39:29Z")

</div>

I don’t think this is true. The manual [says](https://docs.julialang.org/en/v1/manual/types/#man-types-1):

> Both abstract and concrete types can be parameterized by other types. They can also be parameterized by symbols, by values of any type for which [`isbits`](https://docs.julialang.org/en/v1/base/base/#Base.isbits) returns true (essentially, things like numbers and bools that are stored like C types or `struct` s with no pointers to other objects), and also by tuples thereof. Type parameters may be omitted when they do not need to be referenced or restricted.

---

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [July 13, 2020, 7:43pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/7 "2020-07-13T19:43:00Z")

</div>

Alas:

```julia
julia> isbits(big(100))

false

```

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 13, 2020, 7:46pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/9 "2020-07-13T19:46:03Z")

</div>

```julia
struct Mod{N}
    val:: N
    function Mod(x::T, N::S) where {S<:Integer, T<:Integer}
        new{S}(mod(x,N))
    end
end

Mod(10,big(20))

```

You need to give the type parameter S as type to new.  
In addition, you shoud define the type of val to be N, otherwise it would be of type Any.

---

<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:** [July 13, 2020, 7:54pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/10 "2020-07-13T19:54:52Z")

</div>

> [@lungben](#):
>
> `new{S}(mod(x,N))`

I’m pretty sure this is a solution to the wrong problem. Unless I’m mistaken, @scheinerman is trying to make the _value_ `big(20)` a type parameter, not the _type_ `BigInt`. That’s not possible for the reasons posted [above](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/6).

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 13, 2020, 8:01pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/11 "2020-07-13T20:01:41Z")

</div>

Thanks for the clarification and sorry for the misunderstanding on my side!

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [July 13, 2020, 8:02pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/12 "2020-07-13T20:02:53Z")

</div>

`BigInt` is technically mutable (because that’s how GMP works), so it can’t be used as a type parameter, which must be immutable. However, if you just need something slightly larger than 64 bits, you could look into [https://github.com/rfourquet/BitIntegers.jl](https://github.com/rfourquet/BitIntegers.jl).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 13, 2020, 11:04pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/13 "2020-07-13T23:04:15Z")

</div>

Technically, the problem is not that the type is mutable or immutable but that if it `isbits` or not. An immutable type can return a false value for `isbits` and therefore not be eligible to be a value parameter.

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [July 13, 2020, 11:06pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/14 "2020-07-13T23:06:55Z")

</div>

Yes, but the semantic problem is mostly mutability (or pointers to mutable things). The `isbits` is a bit of an implementation detail that I would like to change: [https://github.com/JuliaLang/julia/issues/33387](https://github.com/JuliaLang/julia/issues/33387)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 13, 2020, 11:24pm UTC](https://discourse.julialang.org/t/can-a-bigint-be-a-type-parameter/42997/15 "2020-07-13T23:24:55Z")

</div>

Very interesting. I did not consider the `Union` case, in fact. I was just pointing that an immutable `struct` can have, in fact, fields of types that are not `isbits`, so by consequence it becomes not `isbits` even being immutable itself. Unless you are using a recursive definition of immutability, of course.
