Type-inference with two variables that must have the same type

With constant propagation of the 2 it already works:

julia> fs(x::Int) = x > 2 ? (1,1) : (1.0, 1.0);

julia> f(x, fs, i) = fs(x)[i];

julia> g(x, fs) = (f(x, fs, 1), f(x, fs, 2));

julia> h() = g(2, fs);

julia> @code_warntype h()
MethodInstance for h()
  from h() @ Main REPL[4]:1
Arguments
  #self#::Core.Const(h)
Body::Tuple{Float64, Float64}
1 ─ %1 = Main.g(2, Main.fs)::Core.Const((1.0, 1.0))
└──      return %1

(I thought it might have been necesarry to force specialization on the function with ::F) where F but doesn’t seem like it is needed.)

1 Like