# How to check for a safe cast?

**URL:** <https://discourse.julialang.org/t/how-to-check-for-a-safe-cast/113853>\
**Category:** General Usage\
**Created:** [May 5, 2024, 2:15am UTC](https://discourse.julialang.org/t/how-to-check-for-a-safe-cast/113853 "2024-05-05T02:15:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![berquist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/berquist/32/14270_2.png) [@berquist](https://discourse.julialang.org/u/berquist)\
**Post date:** [May 5, 2024, 2:15am UTC](https://discourse.julialang.org/t/how-to-check-for-a-safe-cast/113853/1 "2024-05-05T02:15:29Z")

</div>

NumPy has [numpy.can\_cast — NumPy v1.26 Manual](https://numpy.org/doc/stable/reference/generated/numpy.can_cast.html). I’d like to know if it exists in a library before I write something simplified based upon `sizeof`, which I think is what NumPy is mostly doing anyway.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 6, 2024, 12:47am UTC](https://discourse.julialang.org/t/how-to-check-for-a-safe-cast/113853/2 "2024-05-06T00:47:51Z")

</div>

What kind of casting would you like to do? There is `convert` and `reinterpret` as the two main kinds I can think of.

```julia-repl
julia> hasmethod(convert, Tuple{Type{Int},Int8})
true

julia> hasmethod(convert, Tuple{Type{Float64},Int8})
true

julia> hasmethod(convert, Tuple{Type{Float64},Rational})
true

julia> convert(Int, 0x8)
8

julia> convert(Float64, 0x8)
8.0

julia> convert(Float64, 1//8)
0.125

```

---

<div class="post-metadata">

**Author:** ![berquist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/berquist/32/14270_2.png) [@berquist](https://discourse.julialang.org/u/berquist)\
**Post date:** [May 6, 2024, 1:19pm UTC](https://discourse.julialang.org/t/how-to-check-for-a-safe-cast/113853/3 "2024-05-06T13:19:22Z")

</div>

I’m trying to reproduce some Python code, so it needs to be stricter than `convert`. `np.can_cast` doesn’t let you cast to a type whose size is smaller. For example, here is a very incomplete implementation (missing rules for going from ints to floats, among many other things):

```julia
function can_cast(dtype_from::DataType, dtype_to::DataType)
    if dtype_from == dtype_to
        return true
    elseif supertype(dtype_from) == supertype(dtype_to)
        return sizeof(dtype_from) <= sizeof(dtype_to)
    end
    return false
end

```

```julia
@testset "can_cast" begin
    # safe
    @test can_cast(Int32, Int32)
    @test can_cast(Int32, Int64)
    @test !can_cast(Int64, Int32)

    @test can_cast(Float32, Float32)
    @test can_cast(Float32, Float64)
    @test !can_cast(Float64, Float32)

    @test can_cast(UInt32, UInt32)
    @test can_cast(UInt32, UInt64)
    @test !can_cast(UInt64, UInt32)
end

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 6, 2024, 1:39pm UTC](https://discourse.julialang.org/t/how-to-check-for-a-safe-cast/113853/4 "2024-05-06T13:39:42Z")

</div>

I think you may be zooming in too much on this function rather than the problem you are trying to solve. What is the context in which you want to use `can_cast`? My guess is that you are probably looking for something like `promote`, but it’s hard to know for sure.

Note that your implementation currently is missing a lot of edge cases, and will be wrong a lot. `supertype(dtype_from) == supertype(dtype_to)` is not an especially reasonable condition to check becausesupertypes can be nested and adding intermediate supertypes is not a breaking change.
