Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add constructors for identity conversions? #113

Open
JeffBezanson opened this issue Nov 29, 2017 · 5 comments
Open

add constructors for identity conversions? #113

JeffBezanson opened this issue Nov 29, 2017 · 5 comments

Comments

@JeffBezanson
Copy link

Currently we get this:

julia> typeof(.1mm)(.1mm)
ERROR: MethodError: Cannot `convert` an object of type SIUnits.SIQuantity{Float64,1,0,0,0,0,0,0,0,0} to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] SIUnits.SIQuantity{Float64,1,0,0,0,0,0,0,0,0}(::SIUnits.SIQuantity{Float64,1,0,0,0,0,0,0,0,0}) at /home/jeff/.julia/v0.6/SIUnits/src/SIUnits.jl:11

Generally subtypes of Number should be able to handle T(x::T) calls. This causes problems e.g. with ranges of unit quantities:

julia> range(1mm, 0.1mm, 50)
Error showing value of type StepRangeLen{SIUnits.SIQuantity{Float64,1,0,0,0,0,0,0,0,0},SIUnits.SIQuantity{Rational{Int64},1,0,0,0,0,0,0,0,0},SIUnits.SIQuantity{Float64,1,0,0,0,0,0,0,0,0}}:
ERROR: MethodError: Cannot `convert` an object of type SIUnits.SIQuantity{Float64,1,0,0,0,0,0,0,0,0} to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] unsafe_getindex(::StepRangeLen{SIUnits.SIQuantity{Float64,1,0,0,0,0,0,0,0,0},SIUnits.SIQuantity{Rational{Int64},1,0,0,0,0,0,0,0,0},SIUnits.SIQuantity{Float64,1,0,0,0,0,0,0,0,0}}, ::Int64) at ./range.jl:506
@timholy
Copy link
Collaborator

timholy commented Nov 29, 2017

Out of curiosity, are you trying this out of interest in something units-related or is this an example to explore defaults for constructors etc.? Worth cross-referencing Unitful.jl, where the first works and the second fails (though it works if you change the first argument to 1.0mm).

@JeffBezanson
Copy link
Author

I found this by trying to create an AxisArray with SIUnit spacings (it works if you use Unitful), just for demo purposes. Naturally, the construct-vs-convert issue jumped out at me.

It's been a while since I looked in on Unitful.jl, and it is really quite glorious! I might even look the other way on its overloading show(::IO, ::Type{...}) :)
Is there any reason to keep both Unitful and SIUnits around?

@timholy
Copy link
Collaborator

timholy commented Nov 30, 2017

Personally I don't think so. SIUnits was started using a pretty early version of Julia (in particular, before @generated functions), and was awesome given what was possible at the time. But Unitful definitely benefits from leveraging more modern functionality. IMO it's one of the best examples of the need for @generated and one of the best "stressors" of the type system (you may recall I mentioned it briefly at JuliaCon).

Also, @ajkeller34 has been awesome as a designer, developer, and maintainer. Really nice to have this part of the Julia landscape in such good hands, in no small measure because it also frees up @Keno to do other cool stuff.

If @Keno is amenable, we could put a more aggressive redirection in the README.

It's just plain fun to attach units to all your variables and see how many packages choke 🙂. (Beyond code, this also applies on purely theoretical grounds to whole fields of applied mathematics, like about 80% of the widely-used algorithms in optimization theory.) My own packages are not immune from that kind of failure. It's consistently a good test of whether you're thinking about things in a clean way.

@Keno
Copy link
Owner

Keno commented Nov 30, 2017

Please do whatever with this package. It was written for my physics/EE homework, which I don't have any more, so I'm no longer regularly using this package.

@ajkeller34
Copy link

@JeffBezanson @timholy Thank you so much for the very generous words! I'm flattered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants