-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
[Feature Request] Default/fallback value when an item is not available yet #205
Comments
Hi @datvm, I don't see this being something we'd add. The reason T should be nullable is that a value might not be in local storage and we might return null. It doesn't seem much work for a developer to check for null and then decide what to do. Unless I'm missing something? |
Hi, After some consideration you are right that adding the methods is not helping much. Sorry I didn't make myself clear when I wrote the suggestion. Right now the signature of the function is: ValueTask<T> GetItemAsync<T>(string key, CancellationToken cancellationToken = default); which, with Nullable Reference Type, is a bit misleading because usually we don't expect a ValueTask<T?> GetItemAsync<T>(string key, CancellationToken cancellationToken = default); and the result may be null? This way, the compiler would raise the warning to developers that the result may be null, similar to Update: after further considering, this is a breaking change so I think it's unlikely? |
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. |
I made #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 While making the changes, I found an interesting change we may want to make as well: the |
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.
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! 😄 |
Is your feature request related to a problem? Please describe.
Currently, when calling
GetItemAsync<T>(key)
, aNullReferenceException
is thrown ifT
is not nullable.Describe the solution you'd like
There should be an overload for specifying the default value instead of throwing. For example:
Describe alternatives you've considered
An alternative is to return
null
instead of throwing an exception likeJsonSerializer.Deserialize<T>
is doing though honestly that is bad because most of the time, you expect non-null values more.My current workaround:
Additional context
None
The text was updated successfully, but these errors were encountered: