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.

[Feature Request] Default/fallback value when an item is not available yet

See original GitHub issue

Is your feature request related to a problem? Please describe. Currently, when calling GetItemAsync<T>(key), a NullReferenceException is thrown if T is not nullable.

Describe the solution you’d like There should be an overload for specifying the default value instead of throwing. For example:

GetItemAsync<T>(string key, T fallbackValue);
GetItemAsync<T>(string key, Func<T> fallbackValueFunc);
GetItemAsStringAsync(string key, string fallbackValue);
GetItemAsStringAsync(string key, Func<string> fallbackValue);

Describe alternatives you’ve considered An alternative is to return null instead of throwing an exception like JsonSerializer.Deserialize<T> is doing though honestly that is bad because most of the time, you expect non-null values more.

My current workaround:

await GetItemAsync<T?>(key) ?? new(); // Or something to generate the fallback value

Additional context None

Issue Analytics

  • State:open
  • Created 6 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chrissaintycommented, Apr 23, 2023

I made https://github.com/Blazored/LocalStorage/pull/208 PR for this. So I realized the project did not have NRT to begin with so this is not actually a breaking change. Before it could have returned null anyway, we just didn’t know it. I think it’s also okay if you merge it for the next major release only.

I believe it’s still a breaking change as developers could have warning set as errors. In which case marking the return type as nullable could trigger a NRT warning, or in their case, an error.

While making the changes, I found an interesting change we may want to make as well: the KeyAsync(int index) method. Right now it returns string? (null if the index is out of range). Should we keep it as it is right now (Javascript convention, which means developers have to manually check for null), or we throw IndexOutOfRangeException like C# convention (extra code on C# side, this is breaking change).

This is a great spot. Could you raise this as a separate issue? This would also count as a breaking change so at least we’re batching them up! 😄

1reaction
chrissaintycommented, Apr 23, 2023

Thanks for the clarification, @datvm. I agree, the method signature is wrong and misleading. You’re also correct it would be a breaking change. I think this can be done, but I’d like to roll it up with any other breaking changes for the next major release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow developers to specify a default/fallback value for a ...
The main caveat I'm seeing is that expressions like ["==", key, value] currently take two string parameters with vastly different interpretation ...
Read more >
Return a default value if a dictionary key is not available
I know that I can check for the key, but I am looking for something more explicit. Is there a way to just...
Read more >
Enabling Item level language fallback
Field level language fallback is disabled by default, so you must enable it on each relevant website and on the template field definition...
Read more >
Enable and set up language fallback
Enable item-level fallback​​ To enable language fallback on a template, navigate to the relevant template and click the _Standard Values item. On the...
Read more >
Language fallback
Item -level fallback enables you to set up an 'empty' item version in a given language so that it falls back to another...
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