question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion to add SafeStart and SafeEnd extension methods to Interval

See original GitHub issue

Currently, 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:open
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jskeetcommented, Sep 8, 2020

Right. Note that developers would still need to remember to use StartOrNull or SafeStart or whatever we call it - I wouldn’t be changing the existing properties (as that would be a breaking change).

0reactions
malcolmrcommented, Sep 8, 2020

-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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found