-
-
Notifications
You must be signed in to change notification settings - Fork 702
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
Annotate with @nogc most of std.datetime.date's API #5529
Conversation
Thanks for your pull request, @ZombineDev! Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
std/datetime/date.d
Outdated
{ | ||
immutable int[] lastDay = isLeapYear ? lastDayLeap : lastDayNonLeap; | ||
|
||
if (day <= 0 || day > (isLeapYear ? daysInLeapYear : daysInYear)) | ||
throw new DateTimeException("Invalid day of the year."); | ||
assert(0, "Invalid day of the year."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
There's also the trick/proposal to use Singleton's for exceptions (e.g. https://github.com/dlang/druntime/pull/1710/files).
-
Does anyone know the exact status of DIP1008? The DIP status was set to " Post-Preliminary Review Round 1" 26 days ago.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ZombineDev Andrei has been against such breaking changes in the past and the solution that @wilzbach proposed was originally put forward by him.
The problem is that the singleton approach has its own issues and is not really a solution.
Does anyone know the exact status of DIP1008?
AFAIK Walter said it was on hold
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very well aware of dlang/druntime#1710. As a matter of fact I intend to resurrect it, but haven't had much free time lately. However, I strongly believe that throwing is not appropriate in this situation.
Breaking changes are a spectrum and in this case no one's code will cease to compile. On the contrary, there are two immediate benefits:
- More of
std.datetime.date
become usable in@nogc nothrow
code -> a game enabler - People who have used anti-patterns such as
catch (Throwable)
orcatch (Exception)
would finally be able to fix lurking problem in their code.
@andralex, please advice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest this be moved anyway outside this PR because it changes semantics, whereas everything else doesn't. For the matter in question, it seems excessive to assert(0) on wrong user input. We don't do that in most places.
8694500
to
c88f896
Compare
I amended the commit to address CircleCI's warnings about line length. |
std/datetime/date.d
Outdated
{ | ||
immutable int[] lastDay = isLeapYear ? lastDayLeap : lastDayNonLeap; | ||
|
||
if (day <= 0 || day > (isLeapYear ? daysInLeapYear : daysInYear)) | ||
throw new DateTimeException("Invalid day of the year."); | ||
assert(0, "Invalid day of the year."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest this be moved anyway outside this PR because it changes semantics, whereas everything else doesn't. For the matter in question, it seems excessive to assert(0) on wrong user input. We don't do that in most places.
It would not be as simple because,
@andralex how about changing this to: assert(day <= 0 || day > (isLeapYear ? daysInLeapYear : daysInYear),
"Invalid day of the year."); Since this is not user-input, but rather a programmer error? |
Write a separate private function that doesn't throw and use it from both the throwing function and the rest of the API. |
If the condition was trivial, e.g. |
…yOfYear * Keep throwing on invalid input in the public API * Use assert for Date.this(), where exceptions are not appropriate
c88f896
to
8ef9c2f
Compare
@ZombineDev close enough but now that you asked it strikes me as overengineered. The path of least resistance is: // returns whether the input worked or not
private bool setDayOfYearImpl(int day) { ... }
... use it ... |
@andralex But, now I would need to duplicate the checking of result on all call sites... |
@ZombineDev point taken |
So @andralex, any other requests, before this is good to go? |
Thanks for the quick review, @andralex! |
This PR isn't even two hours old. Only trivial PRs should be merged quickly. Otherwise we should try to give reviewers appropriate time to comment. |
I think we're good here - it's just adding attributes. |
No, we add a hack to |
@wilzbach my understanding is the change is backward compatible and legit. So the short of it is I gave the PR a review and accepted it. It doesn't have to roast any longer unless the review was superficial. Move fast! |
That's exactly why I want to wait a bit and give other people time to propose better solutions
It's a precedent in the
At Phobos alone there are 110 other PRs in the queue - many solely depend on your approval for new symbols. Please let's not waste more time discussing why we shouldn't merge (dangerous) precedents within two hours and no time for anyone to object. Don't worry - I will personally ensure that this PR finds its way in if there are no objections within a reasonable review time frame. |
To clarify: I think that anything but removing exceptions is sub-optimal. (But that's a topic for the newsgroup.) However, given the backwards compatibilty constraints, I think that my solution is optimal (otherwise, I wouldn't have proposed it).
I can't object giving people more time to review - this can only make the final result better and I'm all for this. However @wilzbach I really don't understand why you think that this is setting a "dangerous precedent". Given that there's no breaking changes (but only a small internal optimization), how do you think we can do better? What's so dangerous about this PR? (Adding Singleton to druntime is on my to do list, but in this case using exceptions internally is totally inappropriate.) |
yup yup point taken @wilzbach thx |
I'm completely against changing the public API so that it doesn't use exceptions. However, changing the internals to use a helper function that asserts instead of having the caller catch I don't know why all of the attributes are getting rearranged here though. I guess that it doesn't really matter, but I'd used a consistent ordering throughout std.datetime and this screws with that for no apparent reason. As for merging stuff quickly, I think that we need to be careful about that, much as we don't want to leave stuff sitting around like we too often do either. Some truly trivial stuff is likely fine, but occasionally, something seemingly simple that someone thought was fine and merged quickly turned out to be a big problem for one reason or another, and there have been complaints in the past about PRs that are merged in less than 24 hours. |
Since I used a semi-automated approach (vim), consistency was important for me too. I noticed that you placed function attributes in the order
While after this PR the ratio gets closer to 1: 0.5:
So it seems the majority of code in |
I brought it up, because I don't really like seeing all that rearranged and would prefer that that sort of thing generally be left alone, particularly when changing it doesn't actually do anything useful, but I approved the PR rather than asking for changes precisely because I recognize that it would be annoying to go back and change the PR. If no one brings up any issues with this PR, then I'll merge it tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the holdup - I saw the PR on my phone and overlooked that setDayOfYear
is private
, so there's nothing controversial about this PR.
FWIW at some point we might want to agree on an idiomatic ordering on the attributes (e.g. alphabetical, attributes without |
Alpha
On Fri, Jun 30, 2017 at 7:10 PM Sebastian Wilzbach ***@***.***> wrote:
I noticed that you placed function attributes in the order ***@***.***
<https://github.com/safe>] [pure] [nothrow] ***@***.***
<https://github.com/nogc>] and I've kept it that way.
FWIW at some point we might want to agree on an idiomatic ordering on the
attributes (e.g. alphabetical, attributes without @ first, ...). However,
I guess this might be very controversial, so I am not really keen on
pushing this.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5529 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAill0f1llx4T7GlL--OCzY4QS1TGxoDks5sJYCAgaJpZM4OKvt->
.
--
Please excuse terseness and typos
|
I don't see much point in pushing consistency with the order everywhere either, since it's just one more thing that everyone is going to screw up and have CircleCI yell about when it really doesn't matter. It's just that I tried to be consistent with it in std.datetime, and so having it rearranged when there's no technical reason to do so is kind of annoying. I'm pretty sure that ddoc already rearranges them in the actual docs though. |
Nope, it doesn't: https://dlang.org/phobos-prerelease/std_datetime_date.html
As said, I am not going for push this. The question/ida was really just to define it in the DStyle, s.t. if in doubt, a common "idiomatic" ordering can be picked. |
Actually, looking at that, it definitely does. For one, it always puts the attributes on the left-hand side of the signature, regardless of which side they're on in the code. And for another, take a look at https://github.com/dlang/phobos/blob/master/std/datetime/date.d#L2800 has https://dlang.org/phobos-prerelease/std_datetime_date.html#.DateTime.julianDay has http://dlang.org/phobos/std_datetime.html#.DateTime.julianDay has the same order (except without the newly added |
FWIW, DDox also rearranges them: long julianDay() pure nothrow @property @nogc @safe const https://dlang.org/library-prerelease/std/datetime/date/date.julian_day.html |
|
The only controversial part of this PR is indayOfYear
. There I changedthrow new DateTimeException(msg)
toassert(0, msg)
, because that prevented a lot of stuff from being @nogc. Even though this technically is a breaking change, I would regard this use of exceptions inappropriate as it is meant to catch a programmer error, rather than incorrect user input. From the programmer's side of view, it is trivial to validate the user's input simply by usingstd.datetime.date.yearIsLeapYear
and comparing with eitherdaysInLeapYear
(366) ordaysInYear
(365).(Speaking of which,
daysInLeapYear
anddaysInYear
should probably be made public.)Edit: I changed this PR so that exceptions were changed to asserts only internally. The external API still uses exceptions, so there are no backwards incompatible changes now. See 8ef9c2f for more details.
Most of what's left are the constructors and setters, because they throw exceptions when the value(s) is(are) out of range. If @jmdavis is ok with changing the rest of the exceptions to asserts (where appropriate) I'll send another PR and soon
std.datetime.date
will be completely GC free.