Suggestion to add SafeStart and SafeEnd extension methods to Interval
See original GitHub issueCurrently, we have usages of the Interval
struct involving missing Start
/End
. The code eventually consolidates the presence/absence of these Instant
values into separate fields. However, boilerplate checks have to be added before accessing Interval.Start
or Interval.End
because of the potential InvalidOperationException
.
We have thus implemented convenience methods for these scenarios:
public static T SafeStart<T>(
this Interval interval,
Func<Instant, T> startInstantSelector,
T replace = default) => interval.HasStart
? startInstantSelector(interval.Start)
: replace;
public static T SafeEnd<T>(
this Interval interval,
Func<Instant, T> endInstantSelector,
T replace = default) => interval.HasEnd
? endInstantSelector(interval.End)
: end;
I suspect this is a very common use case and these methods are generic enough to be included in the NodaTime library itself.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Issues · nodatime/nodatime
Extend ISO 8601 to Support for 2 A and 2 B hours for LocalDateTime to ... Suggestion to add SafeStart and SafeEnd extension...
Read more >Tetra SafeStart Aquarium Starter - with live nitrifying ...
Contains specially bred, live nitrifying bacteria proven to reduce harmful ammonia and nitrite in aquariums; Reduces the ammonia content by up to 14...
Read more >vis-timeline
scaleOffset = 1 - event.scale;\n newStart = safeStart;\n newEnd = safeEnd;\n ... ways until an invisible item is found, we only look at...
Read more >Tender Enquiry Ref No: SR6/RM690S2992 Date: 02.08.2021
SURFACE PREPARATION AND PAINTING OF DRILL FLOOR USING ROPE ACCESS METHOD DRILL FLOOR (100 % area) has to be cleaned to remove dust,....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Right. Note that developers would still need to remember to use
StartOrNull
orSafeStart
or whatever we call it - I wouldn’t be changing the existing properties (as that would be a breaking change).-0.5 to calling this
SafeStart
, fwiw; it’s not really any more or less safe than the existing methods.StartOrNull
seems reasonable.I do wonder how much a method like this would end up being used, and whether it’s worth expanding the API to do so (and writing private extension methods to add those two methods are pretty straightforward).