# Missing of a certain data type

**URL:** <https://discourse.julialang.org/t/missing-of-a-certain-data-type/20779>\
**Category:** General Usage\
**Created:** [February 14, 2019, 7:30am UTC](https://discourse.julialang.org/t/missing-of-a-certain-data-type/20779 "2019-02-14T07:30:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmetumutdurmus](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@ahmetumutdurmus](https://discourse.julialang.org/u/ahmetumutdurmus)\
**Post date:** [February 14, 2019, 7:30am UTC](https://discourse.julialang.org/t/missing-of-a-certain-data-type/20779/1 "2019-02-14T07:30:34Z")

</div>

Hello,

I have just read the documentation on [missing values](https://docs.julialang.org/en/v1/manual/missing/index.html) and while I can’t comment on performance issues I have found it very intuive. The documentation on missing starts as:

`Julia provides support for representing missing values in the statistical sense, that is for situations where no value is available for a variable in an observation, but a valid value theoretically exists. `

Propagation on mathematical operations, behavior in equality, comparison and logical operators are a natural extension of this definition and they make intuitive sense. My question is this: The result of the following operation `0 * missing` is `missing`. This also makes sense if the missing object has a theoretical value of any data type. What if we have a missing value yet theoretically we know it should be a real or integer number. Then the result of the above operation should be `0`. Is there any way to impose a data type such as `Float64` on the `missing` object? If not is this a valid and intuitive request?

For example a `missing(Float64)` object should behave as the following in these two operations: `missing(Float64) * 0` should yield `0`, yet `missing(Float64) * 1` should yield `missing`.

Thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 14, 2019, 7:44am UTC](https://discourse.julialang.org/t/missing-of-a-certain-data-type/20779/2 "2019-02-14T07:44:59Z")

</div>

> [@ahmetumutdurmus](#):
>
> `missing(Float64) * 0` should yield `0` , yet `missing(Float64) * 1` should yield `missing` .

Among other things, this would violate type stability. But that’s no longer that big of a deal,  
and there is no single best concept for missingness, the one used in `Base` is just simple and consistent. So you could define your own type for this, eg

```julia
struct MissingNumber end
const missing_number = MissingNumber()
Base.:*(::MissingNumber, x::Number) = iszero(x) ? x : missing_number
Base.show(io::IO, ::MissingNumber) = print(io, "missing_number")

julia> missing_number * 1
missing_number

julia> missing_number * 0
0

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 15, 2019, 8:58pm UTC](https://discourse.julialang.org/t/missing-of-a-certain-data-type/20779/3 "2019-02-15T20:58:57Z")

</div>

Note that `0 * x` isn’t guaranteed to return zero when `x` is a `Float64`. For example, `0 * Inf` gives `NaN`.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 15, 2019, 9:34pm UTC](https://discourse.julialang.org/t/missing-of-a-certain-data-type/20779/4 "2019-02-15T21:34:57Z")

</div>

It’s a bit off-topic, but was it every considered to have `Missing{T}`? Then one could give more satisfying answers to:

```julia
julia> occursin(missing, "a")
ERROR: MethodError: no method matching occursin(::Missing, ::String)
Stacktrace:
 [1] top-level scope at none:0

julia> zero(missing)
missing

julia> length(missing)
ERROR: MethodError: no method matching length(::Missing)
Closest candidates are:
  length(::Core.SimpleVector) at essentials.jl:582
  length(::Base.MethodList) at reflection.jl:732
  length(::Core.MethodTable) at reflection.jl:806
  ...
Stacktrace:
 [1] top-level scope at none:0

```

It’s occasionally a pain that `f.(v)` calls `f(::Missing)`, which loses the type information in `v`. I get that it might not be worth the extra complexity though…

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 15, 2019, 10:31pm UTC](https://discourse.julialang.org/t/missing-of-a-certain-data-type/20779/5 "2019-02-15T22:31:37Z")

</div>

Yes, it’s been discussed a lot (like probably all issues regarding missing values). The issue it raises has even been dubbed the “counterfactual return type problem” [by John Myles White](https://github.com/JuliaLang/Juleps/blob/jmw/nullable/Nullable.md). Basically, in many situations, a function which wants to return a `Missing{T}` value is not able to find out what `T` is when the value is missing without relying on type inference. But it’s generally not considered a good practice to rely on inference for user-visible behavior, since it can sometimes fail or bail out and return a broad type like `Any`: so you wouldn’t be able to rely on `T` being concrete, which makes it mostly useless.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 15, 2019, 10:37pm UTC](https://discourse.julialang.org/t/missing-of-a-certain-data-type/20779/6 "2019-02-15T22:37:34Z")

</div>

Oh, right. It would have all the same problems Nullable had. Thank you for the answer.
