On Sat, Oct 15, 2016 at 6:21 PM, jw3126 <jw3...@gmail.com> wrote: > > > On Sunday, October 16, 2016 at 12:12:14 AM UTC+2, Yichao Yu wrote: >> >> >> >> 2016-10-15 18:06 GMT-04:00 jw3126 <jw3...@gmail.com>: >> >>> myop(::Int16, ::Int16) = Int32(1) >>> myop(::Int16, ::Int32) = Int64(1) >>> myop(::Int16, ::Int64) = Int128(1) >>> myop(::Int16, ::Int128) = Int128(1) >>> >>> foldr(myop, Int16[1]) |> typeof |> println >>> foldr(myop, Int16[1,1]) |> typeof |> println >>> foldr(myop, Int16[1,1,1]) |> typeof |> println >>> foldr(myop, Int16[1,1,1,1]) |> typeof |> println >>> >>> >>> gives >>> >>> >>> Int32 >>> Int64 >>> Int128 >>> Int128 >>> >>> Would it be better if the answer was typestable (always Int128)? See >>> also here <https://github.com/JuliaLang/julia/issues/18960>. >>> >> >> Yes it would be better if you implement your operation that way and no >> the compiler cannot do this. >> > > One could change the promotion in foldr to use something like the > following: > > accumulate_eltype(op, T) = accumulate_eltype(op, T, T) >
The `promote_op` business is messed up enough. I don't think we need yet another promotion system to make it harder to write code in most cases. > > function accumulate_eltype(op, T, S) > S_next = promote_op(op, T, S) > if S == S_next > return S > else > return accumulate_eltype(op, T, S_next) > end > end > >> >> >>