# Avoid explicit conversion from scientific notation to int for generic struct

**URL:** <https://discourse.julialang.org/t/avoid-explicit-conversion-from-scientific-notation-to-int-for-generic-struct/78739>\
**Category:** General Usage\
**Tags:** question, parametric-types\
**Created:** [March 30, 2022, 1:24pm UTC](https://discourse.julialang.org/t/avoid-explicit-conversion-from-scientific-notation-to-int-for-generic-struct/78739 "2022-03-30T13:24:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uroc327](https://avatars.discourse-cdn.com/v4/letter/u/eb8c5e/32.png) [@Uroc327](https://discourse.julialang.org/u/Uroc327)\
**Post date:** [March 30, 2022, 1:24pm UTC](https://discourse.julialang.org/t/avoid-explicit-conversion-from-scientific-notation-to-int-for-generic-struct/78739/1 "2022-03-30T13:24:07Z")

</div>

Generic structs need an explicit conversion from double to int whereas non-generic don’t. I find this a bit confusing due to the inconsistency. Is there a way to avoid the explicit conversion?

Example:

```julia
struct A
    a::Int
end

struct B{T}
    a::T
    b::Int
end

A(1e5) # works
B(3, 1e5) # doesn't work -> Method error: no matching B(::Int64, ::Float64)
B(3, Int(1e5)) # works

```

I’m usually quite a fan of the fact, that in julia there are fewer rules than in other languages, beacause many things are treated the same. For example that variables and types and functions all just something with a name or that global functions and nested functions and anonymous functions and functions with closures are all just types.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 30, 2022, 1:42pm UTC](https://discourse.julialang.org/t/avoid-explicit-conversion-from-scientific-notation-to-int-for-generic-struct/78739/2 "2022-03-30T13:42:58Z")

</div>

> [@Uroc327](#):
>
> Is there a way to avoid the explicit conversion?

You can add your own constructor:

```julia
B(a::T, b::Number) where T = B{T}(a, Int(b))

```

but it does seem like this could be done automatically. Maybe file an issue?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 30, 2022, 1:56pm UTC](https://discourse.julialang.org/t/avoid-explicit-conversion-from-scientific-notation-to-int-for-generic-struct/78739/3 "2022-03-30T13:56:21Z")

</div>

There’s already an issue for this somewhere. It’s a pretty old one because it’s not entirely clear how to generically generate an outer constructer for a parametric type like this.
