Keno / SIUnits.jl

Efficient unit-checked computation
Other
70 stars 26 forks source link

improve type stability? #109

Open stevengj opened 7 years ago

stevengj commented 7 years ago

Currently, it seems like most of the operations are type-unstable:

julia> using SIUnits, SIUnits.ShortUnits, Base.Test

julia> @inferred 1m
ERROR: return type SIUnits.SIQuantity{Int64,1,0,0,0,0,0,0,0,0} does not match inferred return type Union{Int64,SIUnits.SIQuantity{T<:Number,m,kg,s,A,K,mol,cd,rad,sr}}
 in error(::String) at ./error.jl:21
 in error(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?

julia> x = 1m
1 m

julia> @inferred x*x
ERROR: return type SIUnits.SIQuantity{Int64,2,0,0,0,0,0,0,0,0} does not match inferred return type Union{Int64,SIUnits.SIQuantity{T<:Number,m,kg,s,A,K,mol,cd,rad,sr}}
 in error(::String) at ./error.jl:21
 in error(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?

It seems like there is enough information available to determine these exponents at compile time.

stevengj commented 7 years ago

I was playing around with it, and generated functions seem capable of doing this nicely.

timholy commented 7 years ago

This already works today in Unitful, which has many other nice goodies besides.

stevengj commented 7 years ago

Ah, I see... I guess SIUnits is obsolete then?

timholy commented 7 years ago

It still works and remains an impressive package, but

stevengj commented 7 years ago

Sure, but it would be nice if packages that are superseded by newer packages would be clearly marked as such, e.g. on pkg.julialang.org and in the first sentence of the README.