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

Dates defines nonsensical conversions #19896

Closed
timholy opened this issue Jan 6, 2017 · 4 comments
Closed

Dates defines nonsensical conversions #19896

timholy opened this issue Jan 6, 2017 · 4 comments
Labels
dates Dates, times, and the Dates stdlib module

Comments

@timholy
Copy link
Member

timholy commented Jan 6, 2017

I've posted this kind of thing elsewhere about other unitful quantities, but just noticed that we have the same problem with dates. Succinct example:

julia> d = convert(Dates.Day, 1)
1 day

julia> convert(Int, d)
1

Seems OK, right? But consider:

julia> s = convert(Dates.Second, d)
86400 seconds

julia> convert(Dates.Second, convert(Int, d))
1 second

You can get two wildly different answers when you convert days to seconds this way. The only sensible approach is not to define conversions between unitless and unitful objects.

@tkelman tkelman added the dates Dates, times, and the Dates stdlib module label Jan 6, 2017
@nalimilan
Copy link
Member

Or to define such conversions only in one direction (probably the one from unitful to unitless?).

@TotalVerb
Copy link
Contributor

TotalVerb commented Jan 6, 2017

See also related issues #16109, #10451, #15394.

@timholy
Copy link
Member Author

timholy commented Jan 6, 2017

@nalimilan, I don't think they can be defined at all. Consider:

julia> d = convert(Dates.Day, 1)
1 day

julia> d == 1
false

Saying that you can convert one object to another type, but then to have those two not be equal, is pretty confusing. One should force people to explicitly use a constructor (Dates.Day(1)), which is a computer science construct, not something with physical meaning.

@martinholters
Copy link
Member

Attaching units and removing specific units are reasonable operations, but just removing any units from a quantity is usually dangerous and should not be easily available. "I know the computation lasted for 6.2, but I don't care whether it were seconds or hours."---That doesn't seem right.

And I also agree that attaching units should not be a convert, but maybe a constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dates Dates, times, and the Dates stdlib module
Projects
None yet
Development

No branches or pull requests

5 participants