# Strange type-inference

**URL:** https://discourse.julialang.org/t/strange-type-inference/32334
**Category:** General Usage
**Created:** [December 16, 2019, 12:46pm UTC](https://discourse.julialang.org/t/strange-type-inference/32334 "2019-12-16T12:46:15Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![tomhaber](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@tomhaber](https://discourse.julialang.org/u/tomhaber)
#### Post date: [December 16, 2019, 12:46pm UTC](https://discourse.julialang.org/t/strange-type-inference/32334/1 "2019-12-16T12:46:15Z")

</div>

Hey,

While trying to optimize some code, I ran into some strangely inferred types. The actual example behaves slightly different, but the following excerpt is equally strange.

```julia
abstract type Node end

mutable struct JobNode <: Node
    parent::Node
    theta::Vector{Float64}
end

mutable struct RootNode <: Node
    theta::Vector{Float64}
end

theta(node::Union{RootNode, JobNode}) = node.theta

```

Calling `Base.return_types(theta, (Node,))` gives me `Union{Array{Float64,1}, Node}`  
Can anyone explain this? In my actual code, it returns Any. Is there a better way to do this kind of thing? `code_warntype` also leads to the same result.

Thanks

---

<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: [December 16, 2019, 12:49pm UTC](https://discourse.julialang.org/t/strange-type-inference/32334/2 "2019-12-16T12:49:40Z")

</div>

> [@tomhaber](#):
>
> Can anyone explain this?

Sure, it didn’t propagate the `:theta` constant into the field access, but it was able to figure out that any field access into an object that’s a subtype of `Node`, would return `Union{Array{Float64,1}, Node}`, because that’s the supertype of any field in any subtype of `Union{RootNode, JobNode}`. In general constant propagation doesn’t happen if there is more than one applicable method, though maybe it should be adjusted in this case to be more aggressive.
